...

View Full Version : Why is document.getElementById("id"); not working



alcatraz678
09-13-2008, 05:34 AM
hey guys, I'm a new javascript programmer, i need some help in DOM. I'm having problem with this code, it didn't work out as i planned it to be. the problem is my document.getElementById("baldwins"); is not working in firefox. Firefox doesn't detect the codes, or id.

It's a simple problem, a lot of you guys know this already, I assume..



baldwins = document.getElementById("baldwins");
if (baldwins) {
alert("there is baldwins");
}




<!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" lang = "en-US">
<head>
<title>Simply Javascript</title>
<meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" />
<script type = "text/javascript" src = "minelabs.js"></script>
</head>
<body>
<ul name="baldwins" id="baldwins">
<li id="im>
Alec
</li>

<li>
Daniel
</li>

<li>
William
</li>

<li>
Stephen
</li>
</ul>
</body>
</html>

abduraooft
09-13-2008, 08:06 AM
Guess you've inserted the above javascript snippet directly(without any function) inside minelabs.js, and which is causing the error.

(If so), this snippet will be executed before loading the other html elements inside body, and thus it'd give an error. You'd need to delay this operation, and execute it after the specified element is loaded. So either move it in to a named function and call it in the onload event of body, or simply


window.onload=function (){
baldwins = document.getElementById("baldwins");
if (baldwins) {
alert("there is baldwins");
}
}

rangana
09-13-2008, 02:04 PM
Together with that ammendment, you miss highlighted:


<li id="im">

alcatraz678
09-14-2008, 08:01 AM
Thanks for both of you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum