Jump to content

[SOLVED] getElementById returning "null"


irken

Recommended Posts

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 File

Utilities = { };

Utilities.getElement = function(i)
{
    return document.getElementById(i);
}
[/code]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.