WebandAvec
WebandAvec

Reputation: 5

jQuery change class of td based on next elemt

I have a table output that might look like this:

<tr class="parent" id="someunique1">
<td class="more">IP</td>
<td>blabla</td><td>131313</td><td>04.04.2014, 22:51:02</td>
<td class="redtr">Yup</td>
</tr>

<tr class="parent" id="someunique2">
<td class="more">IP</td>
<td>blaabla</td><td>blarblar</td><td>04.04.2014, 11:33:47</td>
<td>Nope</td>
</tr>

<tr class="child">

Using jQuery, how could I remove the class "more" from the first td under the first parent tr, if the next tr class is another parent?

In the example above I want to keep "more" as a class for the second tr, but not the first, since it's followed bu another parent tr.

Any help appreciated :-)

Upvotes: 0

Views: 81

Answers (4)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$('.parent + .parent').prev().find('td.more').removeClass('more')

Demo: Fiddle

Upvotes: 2

Serge K.
Serge K.

Reputation: 5323

Try this :

$('tr').each(function(index, element) {
    if ($(this).next('tr').hasClass('parent')) {
         $(this).find('.more').removeClass('more');   
    }
});

For each tr, we check if the next tr own the parent class, and if so, we just remove the more class in its child.

Upvotes: 0

Joris Blaak
Joris Blaak

Reputation: 398

Try:

$('tr.parent').each(function() {
    if($(this).next('tr').find('.more').length > 0) {
        $(this).find('.more').removeClass('more');        
    }
});

This will loop through every tr with class parent and if the next tr contains an element with the class more it will remove the class from the element with the class more inside the current tr but not from the second since it'll have some other tr as the next one

Upvotes: 0

Robin
Robin

Reputation: 1216

try it like this:

$('#someunique1').children().removeClass('more');

JS Fiddle: http://jsfiddle.net/4yr4R/1/

Upvotes: 0

Related Questions