views:

66

answers:

1

I'm having a problem with trying to find out whether the parent element does not have the element with the specific class like so:

// after clicking on the button inside of the form - get the instance of the parent form

var par = $(this).parent('form');

if(!par.has('warn')) {

// do something

}

Any idea how to achievie it - as has() doesn't seem to find it

+1  A: 

.has doesn't return a boolean, so if there are no matches the returned object has 0 members.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
</head>
<body>
    <form>
        <div><input id="thebutton" type="button" value="Click Me" /></div>
        <div class="test">test div</div>
    </form>
<script>

$(document).ready(function() {
    $('#thebutton').click(function() {
        var par = $(this).parent('form');
        if(par.has('.warn').length === 0) {
            // do something
            alert('nothing');
        }
    });
});

</script>

</body>
</html>
Jonathan
you are right, but `has` shouldn't be used like this eaither - it will just check if the *form* has the class, and remove it from the set if it doesn't.
Kobi
according to the doco (http://api.jquery.com/has/) has checks the decedents. see the listed example there for more.
Jonathan
You are correct.
Kobi