Jay-oh
Jay-oh

Reputation: 456

If td has value "No" add class to different td

I have the following code:

<td class="bedrag">
    #_ATT{Factuur bedrag}
</td>
<td class="paid">
    #_ATT{Betaald}{Ja|Nee}
</td>

In the td paid the option can be given for Yes or No. I would like change the background of bedrag depending on what was chosen in the td paid. I figured the best way to go was to addClass using Javascript. So I googled around for a while and found this piece of code:

jQuery('a:contains("Sponsored")').closest('.post-item').addClass('sponz'); 

I changed it to fit my needs:

$('.paid:contains("Nee")').closest('.bedrag').addClass('sponz');

But that didn't work. So I tried the following code:

$('.paid:contains("Nee")').addClass('sponz');

This adds the class sponz to the td paid. But I want to add it to bedrag. So I then tried this:

('.bedrag:has(.paid:contains("Nee"))').addClass('sponz');

But this also didn't work.

Anyone know how to get this code working so that it will add sponz the the td bedrag when a user selects the option Nee? thanks in advance!

Upvotes: 1

Views: 343

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

closest() isn't quite what you need as that looks for parents. .bedrag is a sibling of .paid, so you need to use prev() instead:

$('.paid:contains("Nee")').prev('.bedrag').addClass('sponz');
.sponz { color: #C00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="bedrag">bedrag 1</td>
    <td class="paid">Nee</td>
  </tr>
  <tr>
    <td class="bedrag">bedrag 2</td>
    <td class="paid">Ja</td>
  </tr>
  <tr>
    <td class="bedrag">bedrag 3</td>
    <td class="paid">Nee</td>
  </tr>
</table>

Upvotes: 4

Axnyff
Axnyff

Reputation: 9954

Can't you just do an if?

if ($('.paid:contains("Nee")').length !== 0) { // there's at least an element
   // add class to the right element
}

Upvotes: 1

Related Questions