griegs
griegs

Reputation: 22760

jQuery select parent

I have this html;

<div class="AttachmentContainer">
  <div class="display-label">Attachment name</div>
  <div class="display-field">
    <input type="checkbox" id="chkAttachment" class="Attachment" /> 
    <%= thisAttachment.filename%>                                
  </div>
</div>

And on click of the checkbox i have this jQuery code;

$(this).parent(".AttachmentContainer").hide();

But it doesn't work. If I alert out the html() instead of hide() it's null.

If I change it to;

$(this).parent().parent().hide();

it works fine. I thought putting a selector on the parent would keep moving up until it found the parent with that class name.

I don't want to use parent().parent() so what else is there?

edit

.parents(".... doesn't work either.

Upvotes: 3

Views: 3188

Answers (2)

Arnaud Leymet
Arnaud Leymet

Reputation: 6122

Have a look at parentsUntil

Upvotes: 1

Jamie Dixon
Jamie Dixon

Reputation: 53991

You can use the closest method to acheive this:

$(this).closest('.AttachmentContainer').hide();

Upvotes: 9

Related Questions