Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'style' of null

Tags:

javascript

For some strange reason I get a weird error.

I have an element (which I'd defined with PHP) that eventuly looks like this:

    <span id="l_area_1_5" style="display: inline; ">

    ...some html inputs and stuff....

    </span>

Which I am certain by Inspect Element of the Chrome.

Now when I try to address to it programaticly with this:

document.getElementById("1_area_1_5").style.display = 'none';

I get the Cannot read property 'style' of null error.

I have no idea what the hell that's supposed to mean, any help?

like image 579
eric.itzhak Avatar asked Nov 13 '11 18:11

eric.itzhak


People also ask

How do you fix undefined properties Cannot be read?

The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined . You can fix it by adding an undefined check on the variable before accessing it.

Why is my HTML element null?

This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.

Can't access property addEventListener is null?

The error "Cannot read property addEventListener of null" occurs when we call the addEventListener() method on a DOM element that doesn't exist. To solve the error, use an if statement to check if the DOM element exists before calling the addEventListener() method.

Why does getElementById return null?

HTML DOM Document getElementById() The getElementById() method returns null if the element does not exist.


1 Answers

The error means that the element doesn't exist. It's because you have a typo. You wrote 1 instead of l.

l_area_1_5
1_area_1_5
^
like image 148
Mark Byers Avatar answered Oct 06 '22 07:10

Mark Byers