The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> getElementById problem
Zimbobo
post 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>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 03:08 PM