Skip to content Skip to sidebar Skip to footer

Document.getElementbyId() Returning Null

In the code at the line marked as //Error in comments. I get Javascript error saying: 'Cannot read property style of 'null''. I have no idea what it is not able to find the object.

Solution 1:

You are executing your javascript code BEFORE the <div id="connectedUnregistered" /> was actually created.

Also note that you did not close your <div> with a corresponding </div>.

So move your javascript code to a part below your HTML. Or execute it after the page finished loading. If you are using JQuery you can do:

<script>

    $(document).ready(function() {

        ... your code ...

    });
</script>

Solution 2:

Put your script at the end of the HTML document instead of the beginning, and see if that solves things.

JavaScript can't edit the DOM element because it hasn't been created yet.


Solution 3:

Perhaps try placing this code in a

$(document).ready(function(){

//Code

});

block


Solution 4:

in my case it was because of having this line at the beginning of the jsp/html(whatever) file:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

removing it solved the problem for me(I do not remember what it was doing on my page at first place)


Solution 5:

Also, if in some part of the code you are using document.write it's very common to get null.


Post a Comment for "Document.getElementbyId() Returning Null"