Jump to content

Check if an Element/Object exists


tinmat
 Share

Recommended Posts

Hello,I know that the common way to check whether a certain element/object exists is simply to use a statement like this:if (element) {code} // to perform a task if an element existsif (!element) {code} // to perform a task if an element does not existHowever when I do this - it wont execute/cause an error:if (element==true) // if existsif (element==false) // if doesnt existI ponder why the last method won't work using boolean values. I would be very grateful if anyone could explain me the reasons behind.(I aim to use boolean values on the grounds that it is a lot easier to read/code about)

Edited by tinmat
Link to comment
Share on other sites

Think about the possible return values of, say, document.getElementById, are:a reference to the elementnull if it can't be foundIf statements will equate not-null with true, and so will work as expected. But comparing an object reference to the boolean true doesn't work.

Link to comment
Share on other sites

(I aim to use boolean values on the grounds that it is a lot easier to read/code about)
I think I have to disagree. If you ask me it's a lot easier to read and understand what is meant by this:if (document.getElementById("elID")) {than it is this:if (document.getElementById("elID") == true) {The latter would confuse me because I think of the != or == operators as testing the value of something, which is not what you're trying to do.
Link to comment
Share on other sites

Think about the possible return values of, say, document.getElementById, are:a reference to the elementnull if it can't be foundIf statements will equate not-null with true, and so will work as expected. But comparing an object reference to the boolean true doesn't work.
Yeah, it didn't cross my mind to look at the document.getElementById as a method that either returns a reference or a null. "document.getElementById" delivers a return regardless of any event in the document, that is if it been grounded otherwise - could terminate - you could verify whether it failed or reached my statement or not by comparing it to either true or false - and in case it didn't it would execute my statements. While it does not work like this, comparing it to false/true does not make sense. Am I thinking right?Regardless, thanks for your input (appreciated). Gonna play with "!=null / ==null" from now on. Edited by tinmat
Link to comment
Share on other sites

You can also use the typeof operator:

var el = document.getElementById('some-element');if ((typeof el) == 'undefined'){  alert('not defined');}

If you're going for clarity in the code, that's about as clear as you can get. Just make sure you get the parens right, this won't work:if (typeof el == 'undefined')Because of operator precedence rules, that is the same thing as this:if (typeof (el == 'undefined'))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...