473,785 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simplest getElementById question ever.

Why isn't this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javas cript">
alert(document. getElementById( 'pst'));
</script>
</head>

<body>
<div id="subjects">
<form action="" method="get">1. Would you like to
<div id="pst">
<a href="#">write a supporting opinion</a>, or <a href="#">write a
differing opinion</a>, or <a href="#">write a general comment</a>
</div>
<p id="subject_lis t">&nbsp;</p>
</form>
</div>
</body>
</html>

It's killing me. Is there something obvious I'm missing? Thanks.

Jul 23 '05 #1
10 1423
The document has to load before you can reference document.getEle mentById;

wrap it in a function and call it onload:

<script language="javas cript">
function saywhat(){
alert(document. getElementById( 'pst'));
}
onload=sayWhat;
</script>
Jul 23 '05 #2


Rich Hephner wrote:
Why isn't this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javas cript">
alert(document. getElementById( 'pst'));
</script>


The browser parses the document and executes script blocks while parsing
it so at this point when document.getEle mentById('pst') is called it
returns null as no element with that id attribute value is found. So the
example works as expected if you see an alert dialog with null.
Perhaps you want to use
window.onload = function (evt) {
alert(document. getElementById( 'pst'));
};

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
Rich Hephner wrote on 16 mei 2005 in comp.lang.javas cript:
Why isn't this working?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="javas cript">
alert(document. getElementById( 'pst'));
</script>
</head>

<body>
<div id="subjects">
<form action="" method="get">1. Would you like to
<div id="pst">
<a href="#">write a supporting opinion</a>, or <a href="#">write a
differing opinion</a>, or <a href="#">write a general comment</a>
</div>
<p id="subject_lis t">&nbsp;</p>
</form>
</div>
</body>
</html>

It's killing me. Is there something obvious I'm missing? Thanks.


1 the element does not yet exist at the time your alert is executed.

2 you probably don't want to show the element, but some writable property
of that element, like the tagNeme, some style element or the innerHTML.
IE would return [object] but other browsers could have other ideas.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
Wow. Thanks. That's it. One more question though. How come if I create
an external js file like so:

function test(){
alert(document. getElementById( 'pst'));
}

if(document.get ElementById && document.create TextNode){
window.onload = test();
}

and call it from the head of the first html file, it doesn't work?
Thanks.

Jul 23 '05 #5
Rich Hephner wrote on 16 mei 2005 in comp.lang.javas cript:
Wow. Thanks. That's it.
It would be even better if you qoute the stuff you are replying on.

This is not email, nor a helpdesk service, but usenet, and thousands of
readers now and later the archive, don't know a thing of where you are
relying on.

One more question though. How come if I create an external js file like so:

function test(){
alert(document. getElementById( 'pst'));
}

if(document.get ElementById && document.create TextNode){
window.onload = test();
}

and call it from the head of the first html file, it doesn't work?
Thanks.


The same answer would apply if the first was still in your quote.

Please keep to the Netiqutte.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #6


Rich Hephner wrote:
How come if I create
an external js file like so:

function test(){
alert(document. getElementById( 'pst'));
}

if(document.get ElementById && document.create TextNode){
window.onload = test();
}

and call it from the head of the first html file, it doesn't work?


Well it works, you declare a function named test and then you call it on
the right hand of the expression
window.onload = test();
then the function is called and executes
document.getEle mentById('pst')
which at that time does not find an element with that id and returns
null which is displayed.

What you probably want is
window.onload = test;
or
window.onload = function (evt) {
test();
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #7
--- quote --

Rich Hephner wrote:
How come if I create
an external js file like so: function test(){
alert(document. getElementById( *'pst'));
}
if(document.get ElementById && document.create TextNode){
window.onload = test();
}
and call it from the head of the first html file, it doesn't work?


Well it works, you declare a function named test and then you call it
on
the right hand of the expression
window.onload = test();
then the function is called and executes
document.getEle mentById('pst')
which at that time does not find an element with that id and returns
null which is displayed.

What you probably want is
window.onload = test;
or
window.onload = function (evt) {
test();
}
-- end quote --

Thank you Martin. That's what I needed to here. I'm a long time
javascript programmer, but am just now starting to take full advantage
of the new DOM. Thanks for your patience and your help.

Jul 23 '05 #8
>Rich Hephner wrote on 16 mei 2005 in comp.lang.javas cript:
Wow. Thanks. That's it.
It would be even better if you qoute the stuff you are replying on. This is not email, nor a helpdesk service, but usenet, and thousands ofreaders now and later the archive, don't know a thing of where you are relying on.

One more question though. How come if I create
an external js file like so: function test(){
alert(document. getElementById( *'pst'));
}
if(document.get ElementById && document.create TextNode){
window.onload = test();
}
and call it from the head of the first html file, it doesn't work?
Thanks.

The same answer would apply if the first was still in your quote. Please keep to the Netiqutte.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


Thanks for the scolding. I'm using Google groups for posting because
that's all I have access to at my current location. They don't make it
easy to quote from past postings. I'm well aware of the Netiquette.

Jul 23 '05 #9
Rich Hephner wrote on 16 mei 2005 in comp.lang.javas cript:
Thanks for the scolding. I'm using Google groups for posting because
that's all I have access to at my current location. They don't make it
easy to quote from past postings. I'm well aware of the Netiquette.


Is that a compelling argument not to do your utmost?

I am told it is quite possible.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1344
by: Gerry | last post by:
Hi, I have a javascript function which uses the method document.getElementById. I'm using it to decide whether a checkbox has been ticked or not. This decision is encoded in an if statement with the condtion being if (document.getElementById(checkboxtoupdate).value ==1)
24
9904
by: Robi | last post by:
I have the following problem: I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container. for (i=0; i < MyString.length; i++) document.write('<div id="MyTest" style="position:absolute;top:0px;left:0;height:12;width:12;text-align:center">'+MyString+'</div>'); Now if MyString contains 6 characters, I end up with 6 <div id="MyTest"> containers. With IE I am able to access these containers as...
11
2994
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application as fast as possible. I have about 10-20 id tags that need to be accessed and modified from time to time. Would the jvm perform slowly if I stored all of the dom node strings "document.node.child...." into a huge js array?
0
1192
by: Rico | last post by:
The simplest php webapp ever http://morecute.com
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.