JavaScript element exists

This is a JavaScript Function to check if an element exists, I use it regularly in an If.

The function:

function element_exists (elem) {
  return((elem)?1:0);
}

The test:

//Test1
if(element_exists(document.getElementById("masthead"))){
console.log("element exists");
}else{
console.log("element not exists");
}
//Test2
if(element_exists(document.getElementById("masthead1"))){
console.log("element exists");
}else{
console.log("element not exists");
}

Our result:

element exists
element not exists

We could see it works as it should.

4 thoughts on “JavaScript element exists”

  1. Hi to every one, it’s genuinely a nice for me to
    visit this website, it consists of priceless Information.

  2. I enjoy reading a post that can make people think.
    Also, many thanks for allowing me to comment!

  3. Hi I am so glad I found your blog, I really found you by
    accident, while I was looking on Yahoo for something else, Anyhow I am
    here now and would just like to say thanks
    for a marvelous post and a all round entertaining blog (I also
    love the theme/design), I don’t have time to read it all at the moment
    but I have bookmarked it and also included your RSS feeds,
    so when I have time I will be back to read
    more, Please do keep up the excellent jo.

Leave a Reply to EmileeOHix Cancel reply

Your email address will not be published.

1 × 4 =