tags:

views:

56

answers:

4

Hi,

Suppose the following HTML div container

<div id="container">
    <form>
        <div>
            <label class="error">Name</label>
            <input type="text" name="name"/>
        </div>
        <div>
            <label>Age</label>
            <input type="text" name="age"/>
        </div> 
    </form>
</div>

How can i check whether any child of container div has an error class by using JQuery ?

+2  A: 
$("#container .error").length
daddywoodland
What if it's not a label?
Stefan Kendall
+1  A: 
$('div').children( function(){
if( $(this).hasClass( 'error' ) )
{
//do something
}
} );
Stefan Kendall
+1  A: 
if ($("#container label.error").length == 0) {
  // none
} else {
  // at least one
}
cletus
A: 

I started from the #container:

CODE

jQuery("#container").find("label").each(function (i) {
  if ( jQuery(this).hasClass("error") ) {
    //error exist
  }
});
ranonE