Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is if(document.getElementById('something')!=null) identical to if(document.getElementById('something'))?

When I want to check if an element exists in a page. Are these two checks the same? Is there a better more compact way to check the existence?

What if I want to check if the value == ''. Can this be included in this check as well?

like image 564
Malik Daud Ahmad Khokhar Avatar asked Jul 14 '10 14:07

Malik Daud Ahmad Khokhar


People also ask

Why does document getElementById show null?

getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.

Why document getElementById return null react?

The document. getElementById() method returns null in React when we call the method before the element with the provided ID has been rendered to the DOM or if an element with the ID doesn't exist. To get around this, call the getElementById() method in the useEffect hook. Copied!

What does document getElementById return if not found?

HTML DOM Document getElementById() The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.


2 Answers

A reference to an element will never look "falsy", so leaving off the explicit null check is safe.

Javascript will treat references to some values in a boolean context as false: undefined, null, numeric zero and NaN, and empty strings. But what getElementById returns will either be an element reference, or null. Thus if the element is in the DOM, the return value will be an object reference, and all object references are true in an if () test. If the element is not in the DOM, the return value would be null, and null is always false in an if () test.

It's harmless to include the comparison, but personally I prefer to keep out bits of code that don't do anything because I figure every time my finger hits the keyboard I might be introducing a bug :)

Note that those using jQuery should not do this:

if ($('#something')) { /* ... */ } 

because the jQuery function will always return something "truthy" — even if no element is found, jQuery returns an object reference. Instead:

if ($('#something').length) { /* ... */ } 

edit — as to checking the value of an element, no, you can't do that at the same time as you're checking for the existence of the element itself directly with DOM methods. Again, most frameworks make that relatively simple and clean, as others have noted in their answers.

like image 178
Pointy Avatar answered Sep 20 '22 16:09

Pointy


It's better (but wordier) to use:

var element = document.getElementById('something'); if (element != null && element.value == '') { } 

Please note, the first version of my answer was wrong:

var element = document.getElementById('something'); if (typeof element !== "undefined" && element.value == '') { } 

because getElementById() always return an object (null object if not found) and checking for"undefined" would never return a false, as typeof null !== "undefined" is still true.

like image 40
Andir Avatar answered Sep 18 '22 16:09

Andir