TIMEX
TIMEX

Reputation: 271674

How come .parent() is not working?

<div class="apple">
    <div class="abc">
       <input id="go">
    </div>
</div>


$("#go").click(function(){
    $(this).parent('.apple').hide(); // this doesn't work.
    $(this).parent().parent().hide(); //this works
});

I want the .parent('.apple') to work.

Upvotes: 0

Views: 112

Answers (2)

brendan
brendan

Reputation: 29976

The parent of the input is your div with class abc. That's why it won't work. You want to use parents plural to go up the parent chain:

$("#go").click(function(){

$(this).parents('.apple').hide(); 

});

See this link for more info: http://jqueryminute.com/jquery-parent-vs-parents/

Upvotes: 1

Crozin
Crozin

Reputation: 44376

From jQuery.parent() function docs:

[...] The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree. [...]

In other words use jQuery.parents() instead of jQuery.parent().

Upvotes: 5

Related Questions