irken Posted January 3, 2007 Share Posted January 3, 2007 Hello.I'm struggling to figure out why the code below isn't working. I have my DIV named "testing", I do document.getElementById("testing"); and it returns null. Any ideas?Test here: http://conventia.dk/pixels/index.html[b]EDIT[/b] - I have tried just using <script></script> tags to do my document.getElementById(..) in, returns null aswell.[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" ><head> <title>Pixels</title> <script language="javascript" type="text/javascript" src="javascript/Utilities.js"></script> <script language="javascript" type="text/javascript"> var el = Utilities.getElement("testing"); alert(el); </script></head><body><div id="testing"> <p>Abcd</p></div></body></html>[/code]Utilities.js[code]// JScript FileUtilities = { };Utilities.getElement = function(i){ return document.getElementById(i);}[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 3, 2007 Share Posted January 3, 2007 What about a direct call? Quote Link to comment Share on other sites More sharing options...
irken Posted January 3, 2007 Author Share Posted January 3, 2007 [quote author=fenway link=topic=120887.msg496426#msg496426 date=1167863435]What about a direct call?[/quote]Exactly the same, that's why it's really strange. This procedure is so simple, what can go wrong?In head and body, both returns null.<script>var b = document.getElementById("testing");alert(b); <- null</script>I've tried using various other DOCTYPES, but it's also the same result. There is no error message or anything. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 4, 2007 Share Posted January 4, 2007 try alert(b.innerHTML); Quote Link to comment Share on other sites More sharing options...
irken Posted January 4, 2007 Author Share Posted January 4, 2007 [quote author=emehrkay link=topic=120887.msg496575#msg496575 date=1167881849]try alert(b.innerHTML);[/quote]Returns the same, null object (object error). Hmm ???Doing an alert on document.getElementById (alert(document.getElementById)) returns:[code]function getElementById() { [native code]}[/code]So it is working, but it's unable to find the div. I'm really lost for ideas.[code]var oText = document.getElementById('iText');alert(oText.value);..<input id="iText" type="text" value="hello" />[/code]returns object error aswell.[code] <script language="javascript" type="text/javascript"> try { //var el = Utilities.getElement("testing"); var b = document.getElementById("testing"); alert(b.innerHTML); } catch (err) { alert(err); } </script>[/code] Quote Link to comment Share on other sites More sharing options...
irken Posted January 4, 2007 Author Share Posted January 4, 2007 Argh.. how retarded can one be.The javascript section is getting loaded before the actual <body> itself, so requesting an object that doesn't exist yet is of course going to return null. Putting this piece of code in a function and calling it in the onload property of <body> fixed it.[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head> <title>Pixels</title> <script language="javascript" type="text/javascript" src="javascript/Utilities.js"></script></head><body onload="javascript:alert(Utilities.getElement('testing').innerHTML);"><div id="testing"> <p>Abcd</p></div></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 5, 2007 Share Posted January 5, 2007 Well yes... the browser has to render this object first. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.