Corentin Branquet
Corentin Branquet

Reputation: 1576

Easiest way to target parent Jquery

I've this code :

$(a).parent().parent().parent().addClass('ITWORKS');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <p>
    <span>
      <a href="#">
        Hello world
      </a>
    </span>
  </p>
</div>

Instead of writing this long line of code, How can I target the div only with the link

Thanks for your help !

Upvotes: 1

Views: 15

Answers (1)

JotaBe
JotaBe

Reputation: 39004

Simply use .closest('div.conatiner')

See the docs of .closest() here.

Upvotes: 2

Related Questions