Skip to content Skip to sidebar Skip to footer

Finding Whether The Element Exists In Whole Html Page

I want to check whether an element exists in the whole page or not. Is there any way to know if the element exists in the page through jQuery? For example:

Solution 1:

For element IDs:

if($('#para1').length){
  //element with id exists
}

For element class:

if($('.para_class').length){
  //element with classexists
}

Solution 2:

recently I faced the same problem & this is good for me.

if ( $('#para1').length == 1 ){ // if the id exists its length will be 1 alert('This Id exists');

} elseif ( $('#para1').length == 0 ){ // if the id doesn't exists its length will be 0alert('This Id does not exists');
}

Solution 3:

I know I'm a little late, but, to check if an input doesn't exist with an id using JQuery, you can do this:

if (!$('#inputid').length > 0)) {
}

Post a Comment for "Finding Whether The Element Exists In Whole Html Page"