Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Why is document.getElementById("id"); not working

    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..

    Code:
    baldwins = document.getElementById("baldwins");
    if (baldwins) {
    	alert("there is baldwins");
    }
    Code:
    <!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>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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
    Code:
    window.onload=function (){
    baldwins = document.getElementById("baldwins");
    if (baldwins) {
    	alert("there is baldwins");
    }
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    alcatraz678 (09-14-2008)

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Together with that ammendment, you miss highlighted:
    Code:
    <li id="im">
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for both of you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •