for example I have a div <div id="myrules"></div>
If i use
alert($("#myrules").find("if"))
it alerts [object object]
, even though the div is empty, so the question is how to tell if the child exists or not using .find()
?
for example I have a div <div id="myrules"></div>
If i use
alert($("#myrules").find("if"))
it alerts [object object]
, even though the div is empty, so the question is how to tell if the child exists or not using .find()
?
jQuery returns a set of matched elements, this set is empty if no elements were selected, so you need to check against the length property of the returned value.
// "length" of the returned collection is 0
if(jQuery('#myelem').find('.idontexist').length)
alert("element found");
Also to be found here: http://api.jquery.com/jQuery/
You can check children of an element like:
HTML
<div id="myrules">
<div class="ch">1</div>
<div class="ch">2</div>
<p>3</p>
</div>
jQuery:
alert($("#myrules").children().length) // Alerts total children exists
Also you can check specific total children in an element:
alert($("#myrules").children('div').length)
alert($("#myrules").children('p').length)