-4

Is there a Jquery that checks if an element exists?

I have some JSP that has a div.

Then I have a javascript file that is used by all pages. Inside that file I have an ajaxStart that reset the property of the div.

My problem is an error message is shown in the console of the browser that the element don't exist for the pages that don't have div and I don't want it.

Using .length is not an option since it will still display that the element don't exist in the browser's console.

Empress T
  • 93
  • 12
  • 3
    Possible duplicate of [Is there an "exists" function for jQuery?](http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) – Rino Raj Apr 15 '16 at 06:55
  • [Check if element exists in jQuery](http://stackoverflow.com/questions/4592493/check-if-element-exists-in-jquery) – Rino Raj Apr 15 '16 at 06:55
  • It's somehow different since the ".length" is accepted while it is not the answer I am looking for. – Empress T Apr 25 '16 at 01:39

2 Answers2

-2
var element;
if(element.length > 0){
    console.log('element exist');
}
else{
    console.log('element not exist');
}
Ashish Patel
  • 3,551
  • 1
  • 15
  • 31
-3

simply select the dive and check its .length property

if($('.div').length > 0)
{ 
    //code if div exists
}
Saad Mujeeb
  • 106
  • 1
  • 3
  • While this should work, it does not answer the question, since the author feels using `.length` is not an option. Perhaps you could explain why that assumption is incorrect to make this a more complete answer. – Hans Roerdinkholder Apr 15 '16 at 10:09
  • instead of downvoting, you could have even answered the question too @HansRoerdinholder – Saad Mujeeb Apr 15 '16 at 10:52