Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a div is exists or not? [duplicate]

Tags:

jquery

Possible Duplicate:
Is there an “exists” function for jQuery

 <div class="XXX">
  <div class="created">
  </div>
</div>

div class="created" automatically generated by JavaScript using append function jQuery for some validation i need to check whether div is generated or not how can i do this.using jQuery.

something like $('.xxx').html()==' '

like image 378
Anudeep GI Avatar asked Jul 21 '12 06:07

Anudeep GI


People also ask

How do you check if the DIV is already exists?

$(document). ready(function() { if ($('#DivID'). length){ alert('Found with Length'); } if ($('#DivID'). length > 0 ) { alert('Found with Length bigger then Zero'); } if ($('#DivID') !=

How do you check if an element exists or not?

There are two ways to check whether an element in the HTML document exists or not using jQuery. Approach 1: Using the length property in jQuery. If the element exists, then the length property will return the total count of the matched elements with the specified selector.

How do you check whether a div is present or not in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

How do you check button is exist or not in jQuery?

Answer: Use the jQuery . length Property You can use the jQuery . length property to determine whether an element exists or not in case if you want to fire some event only if a particular element exists in DOM. Here's an example that displays an alert on button click if the specified element exists.


2 Answers

Try this like following:

$('div.XXX div.created').length

if the div is not create then $('div.XXX div.created').length will return 0.

if( $('div.XXX div.created').length > 0 ){
  // do something 
}

In jQuery, it has method .size() and implement like $('div.XXX div.created').size(), but .length is more reliable.

like image 85
thecodeparadox Avatar answered Oct 16 '22 21:10

thecodeparadox


you can use jQuery length property which returns the number of selected elements:

if ($('.XXX div.created').length > 0) {

}
like image 5
undefined Avatar answered Oct 16 '22 20:10

undefined