Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Add class to this element outside of parent div with jQuery

I'm trying to target a specific parent of an element and then add a class to it with jQuery. Example:

$('.disable-link').click(function(e) {
  $(this).hide();
  $(this).parents('.item').find('.title').addClass('disabled');
  
  e.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="item">
  <div class="title">Thing 1</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 1</a>
</div>
<div class="item">
  <div class="title">Thing 2</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 2</a>
</div>
<div class="item">
  <div class="title">Thing 3</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 3</a>
</div>

I thought that by using .parents('.item') it would go all the way up the chain to that specific item class with this and target that specific one, but for some reason it's not.

I've tried using parent().parent().find('.title') but that ends up adding the disabled class to all of the titles and not just the one. I have a feeling that it something simple, but I can't wrap my head around the specific inheritance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The elements with class .item are not a parents of .disable-link elements so you should target the parent div first then get the previous .item using the .prev() method like:

$('.disable-link').click(function(e) {
    $(this).hide();
    $(this).parents('div').prev('.item').find('.title').addClass('disabled');

    e.preventDefault();
});

Hope this helps.

$('.disable-link').click(function(e) {
  e.preventDefault();
  
  $(this).hide();
  $(this).parents('div').prev('.item').find('.title').addClass('disabled');
});
.disabled{
   background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="item">
  <div class="title">Thing 1</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 1</a>
</div>
<div class="item">
  <div class="title">Thing 2</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 2</a>
</div>
<div class="item">
  <div class="title">Thing 3</div>
</div>
<div>
  <a class="disable-link" href="#">Click to Disable Thing 3</a>
</div>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!