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?
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.
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.
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.
HTML DOM Document getElementById() The getElementById() method returns null if the element does not exist.
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
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With