Reputation: 271674
<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
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
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