getElementById problem |
getElementById problem |
Zimbobo |
Dec 5 2009, 06:09 PM
Post
#1
|
Group: Members Posts: 1 Joined: 5-December 09 Member No.: 10,505 |
In the following code document.getElementById returns null. What am I doing wrong?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Curtain</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript"> /* <![CDATA[ */ var b = document.getElementById("box"); window.alert(b); /* ]]> */ </script> </head> <body> <div id="box" style="position: absolute; background-color: blue;"></div> </body> </html> |
pandy |
Dec 5 2009, 06:15 PM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
When the script executes the box hasn't been created yet.
|
Christian J |
Dec 6 2009, 05:56 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
One workaround is to put the script after the box.
Another is to use run the script in a window.onload event. In some cases this is a bit slow, since the whole page must load. |
Lo-Fi Version | Time is now: 23rd April 2024 - 03:08 PM |