What are the possible reasons for document.getElementById
, $("#id")
or any other DOM method / jQuery selector not finding the elements?
Example problems include:
.val()
, .html()
, .text()
) returning undefined
null
resulting in any of several errors:Uncaught TypeError: Cannot set property '...' of null Uncaught TypeError: Cannot read property '...' of null
The most common forms are:
Uncaught TypeError: Cannot set property 'onclick' of null
Uncaught TypeError: Cannot read property 'addEventListener' of null
Uncaught TypeError: Cannot read property 'style' of null
This question is related to
javascript
jquery
dom
the problem is that the dom element 'speclist' is not created at the time the javascript code is getting executed. So I put the javascript code inside a function and called that function on body onload event.
function do_this_first(){
//appending code
}
<body onload="do_this_first()">
</body>
If script execution order is not the issue, another possible cause of the problem is that the element is not being selected properly:
getElementById
requires the passed string to be the ID verbatim, and nothing else. If you prefix the passed string with a #
, and the ID does not start with a #
, nothing will be selected:
<div id="foo"></div>
// Error, selected element will be null:
document.getElementById('#foo')
// Fix:
document.getElementById('foo')
Similarly, for getElementsByClassName
, don't prefix the passed string with a .
:
<div class="bar"></div>
// Error, selected element will be undefined:
document.getElementsByClassName('.bar')[0]
// Fix:
document.getElementsByClassName('bar')[0]
With querySelector, querySelectorAll, and jQuery, to match an element with a particular class name, put a .
directly before the class. Similarly, to match an element with a particular ID, put a #
directly before the ID:
<div class="baz"></div>
// Error, selected element will be null:
document.querySelector('baz')
$('baz')
// Fix:
document.querySelector('.baz')
$('.baz')
The rules here are, in most cases, identical to those for CSS selectors, and can be seen in detail here.
To match an element which has two or more attributes (like two class names, or a class name and a data-
attribute), put the selectors for each attribute next to each other in the selector string, without a space separating them (because a space indicates the descendant selector). For example, to select:
<div class="foo bar"></div>
use the query string .foo.bar
. To select
<div class="foo" data-bar="someData"></div>
use the query string .foo[data-bar="someData"]
. To select the <span>
below:
<div class="parent">
<span data-username="bob"></span>
</div>
use div.parent > span[data-username="bob"]
.
Capitalization and spelling does matter for all of the above. If the capitalization is different, or the spelling is different, the element will not be selected:
<div class="result"></div>
// Error, selected element will be null:
document.querySelector('.results')
$('.Result')
// Fix:
document.querySelector('.result')
$('.result')
You also need to make sure the methods have the proper capitalization and spelling. Use one of:
$(selector)
document.querySelector
document.querySelectorAll
document.getElementsByClassName
document.getElementsByTagName
document.getElementById
Any other spelling or capitalization will not work. For example, document.getElementByClassName
will throw an error.
Make sure you pass a string to these selector methods. If you pass something that isn't a string to querySelector
, getElementById
, etc, it almost certainly won't work.
If the HTML attributes on elements you want to select are surrounded by quotes, they must be plain straight quotes (either single or double); curly quotes like ‘
or ”
will not work if you're trying to select by ID, class, or attribute.
This solution is for the people who don't use jQuery and to improve performance by not moving the script to bottom of the page, and the problem is that the script is loaded before the html elements are loaded. Add your code in this function body
window.onload=()=>{
// your code here
// example
let element=document.getElementById("elementId");
console.log(element);
};
add everything that has to work only after the document is loaded and keep other functions that has to be executed as soon as the script is loaded outside the function.
I recommend this method instead of moving down the script, because if the script is on top, the browser will try to download it as soon as it sees the script tag, if it is on the bottom of the page, it will take some more time to load it and until that time no event listeners in the script will work. in this case all other functions could be called and the window.onload
will get called once everything is loaded.
Reasons why id based selectors don't work
Solutions
Try to access the element after its declaration or alternatively use stuff like $(document).ready();
For elements coming from Ajax responses, use the .bind()
method of jQuery. Older versions of jQuery had .live()
for the same.
Use tools [for example, webdeveloper plugin for browsers] to find duplicate ids and remove them.
Short and simple: Because the elements you are looking for do not exist in the document (yet).
For the remainder of this answer I will use getElementById
as example, but the same applies to getElementsByTagName
, querySelector
and any other DOM method that selects elements.
Possible Reasons
There are two reasons why an element might not exist:
An element with the passed ID really does not exist in the document. You should double check that the ID you pass to getElementById
really matches an ID of an existing element in the (generated) HTML and that you have not misspelled the ID (IDs are case-sensitive!).
Incidentally, in the majority of contemporary browsers, which implement querySelector()
and querySelectorAll()
methods, CSS-style notation is used to retrieve an element by its id
, for example: document.querySelector('#elementID')
, as opposed to the method by which an element is retrieved by its id
under document.getElementById('elementID')
; in the first the #
character is essential, in the second it would lead to the element not being retrieved.
The element does not exist at the moment you call getElementById
.
The latter case is quite common. Browsers parse and process the HTML from top to bottom. That means that any call to a DOM element which occurs before that DOM element appears in the HTML, will fail.
Consider the following example:
<script>
var element = document.getElementById('my_element');
</script>
<div id="my_element"></div>
The div
appears after the script
. At the moment the script is executed, the element does not exist yet and getElementById
will return null
.
jQuery
The same applies to all selectors with jQuery. jQuery won't find elements if you misspelled your selector or you are trying to select them before they actually exist.
An added twist is when jQuery is not found because you have loaded the script without protocol and are running from file system:
<script src="//somecdn.somewhere.com/jquery.min.js"></script>
this syntax is used to allow the script to load via HTTPS on a page with protocol https:// and to load the HTTP version on a page with protocol http://
It has the unfortunate side effect of attempting and failing to load file://somecdn.somewhere.com...
Solutions
Before you make a call to getElementById
(or any DOM method for that matter), make sure the elements you want to access exist, i.e. the DOM is loaded.
This can be ensured by simply putting your JavaScript after the corresponding DOM element
<div id="my_element"></div>
<script>
var element = document.getElementById('my_element');
</script>
in which case you can also put the code just before the closing body tag (</body>
) (all DOM elements will be available at the time the script is executed).
Other solutions include listening to the load
[MDN] or DOMContentLoaded
[MDN] events. In these cases it does not matter where in the document you place the JavaScript code, you just have to remember to put all DOM processing code in the event handlers.
Example:
window.onload = function() {
// process DOM elements here
};
// or
// does not work IE 8 and below
document.addEventListener('DOMContentLoaded', function() {
// process DOM elements here
});
Please see the articles at quirksmode.org for more information regarding event handling and browser differences.
jQuery
First make sure that jQuery is loaded properly. Use the browser's developer tools to find out whether the jQuery file was found and correct the URL if it wasn't (e.g. add the http:
or https:
scheme at the beginning, adjust the path, etc.)
Listening to the load
/DOMContentLoaded
events is exactly what jQuery is doing with .ready()
[docs]. All your jQuery code that affects DOM element should be inside that event handler.
In fact, the jQuery tutorial explicitly states:
As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.
To do this, we register a ready event for the document.
$(document).ready(function() { // do stuff when DOM is ready });
Alternatively you can also use the shorthand syntax:
$(function() {
// do stuff when DOM is ready
});
Both are equivalent.
If the element you are trying to access is inside an iframe
and you try to access it outside the context of the iframe
this will also cause it to fail.
If you want to get an element in an iframe you can find out how here.
As @FelixKling pointed out, the most likely scenario is that the nodes you are looking for do not exist (yet).
However, modern development practices can often manipulate document elements outside of the document tree either with DocumentFragments or simply detaching/reattaching current elements directly. Such techniques may be used as part of JavaScript templating or to avoid excessive repaint/reflow operations while the elements in question are being heavily altered.
Similarly, the new "Shadow DOM" functionality being rolled out across modern browsers allows elements to be part of the document, but not query-able by document.getElementById and all of its sibling methods (querySelector, etc.). This is done to encapsulate functionality and specifically hide it.
Again, though, it is most likely that the element you are looking for simply is not (yet) in the document, and you should do as Felix suggests. However, you should also be aware that that is increasingly not the only reason that an element might be unfindable (either temporarily or permanently).
My solution was to not use the id of an anchor element: <a id='star_wars'>Place to jump to</a>
. Apparently blazor and other spa frameworks have issues jumping to anchors on the same page. To get around that I had to use document.getElementById('star_wars')
. However this didn't work until I put the id in a paragraph element instead: <p id='star_wars'>Some paragraph<p>
.
Example using bootstrap:
<button class="btn btn-link" onclick="document.getElementById('star_wars').scrollIntoView({behavior:'smooth'})">Star Wars</button>
... lots of other text
<p id="star_wars">Star Wars is an American epic...</p>
When I tried your code, it worked.
The only reason that your event is not working, may be that your DOM was not ready and your button with id "event-btn" was not yet ready. And your javascript got executed and tried to bind the event with that element.
Before using the DOM element for binding, that element should be ready. There are many options to do that.
Option1: You can move your event binding code within document ready event. Like:
document.addEventListener('DOMContentLoaded', (event) => {
//your code to bind the event
});
Option2: You can use timeout event, so that binding is delayed for few seconds. like:
setTimeout(function(){
//your code to bind the event
}, 500);
Option3: move your javascript include to the bottom of your page.
I hope this helps you.
Source: Stackoverflow.com