0
<div class="one">
    <div class="two">
        <div class="three">success</div>
    </div>
</div>

If I click the third div, for example $(.three).click(function (){ }); I need the class of the parent div (.one). How can I do this in jQuery?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
balaji-p
  • 1
  • 3

3 Answers3

2

try with: $('.three').parents('.one')
or with $('.three').closest('.one')

from the DOCS: http://api.jquery.com/parents/
from the DOCS: http://api.jquery.com/closest/

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

You could try with .parent().parent(), e.g. $(".three").parent().parent().anything(...).

sp00m
  • 47,968
  • 31
  • 142
  • 252
1

A general solution would be:

$(this).closest(".outer");

so:

$(".anyDepth").click(function () { 
    $(this).closest(".outermostDiv").addClass("foo");
});
karim79
  • 339,989
  • 67
  • 413
  • 406