Why document.getelementbyId doesn't work in Firefox?

I can't figure out why document.getElementById doesn't work in Firefox:

document.getElementById("main").style.width = "100";

      

When I check Firebug it says:

TypeError: document.getElementById ("main") is null

Does anyone know why this is happening?

EDIT: Unfortunately the body element was a bad example. I changed it to another element with the id "main".

+2


source to share


4 answers


You have set the id of the element <body>

to "body":

<body id="body" ...>

      



Update:

Check if the following example works for you: http://jsbin.com/uyeca/edit Click the Output tab to see the output (which should be a 600px wide DIV).

+3


source


put your script before

</body>

      



Or if you are using a script in <head>

, you can change the code:

$(document).ready(function() {
    //enter code here.
});

      

+4


source


https://developer.mozilla.org/En/DOM/Document.getElementById

Just create an element and assigning an ID will not result in an element accessible by getElementById. Instead, one needs to insert the element first into the document tree with an insert or similar, probably in a hidden div.

var element = document.createElement("div");
element.id = 'testqq';
var el = document.getElementById('testqq'); //

      

el will be null!

+3


source


I had the same problem ... I was trying to use "getElementById" without the main structure of the HTML page.

Once added to my page, it worked fine ... I was working on a script that needed to be embedded in other sites - widgets.

0


source







All Articles