This is a JavaScript Function to check if an element exists, I use it regularly in an If.
The function:
1 2 3 |
function element_exists (elem) { return((elem)?1:0); } |
The test:
1 2 3 4 5 6 7 8 9 10 11 12 |
//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:
1 2 |
element exists element not exists |
We could see it works as it should.
Hi to every one, it’s genuinely a nice for me to
View Commentvisit this website, it consists of priceless Information.
I enjoy reading a post that can make people think.
View CommentAlso, many thanks for allowing me to comment!
I used to be able to find good advice from your blog posts.
View CommentHi I am so glad I found your blog, I really found you by
View Commentaccident, 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.