MShack
MShack

Reputation: 652

How to add tag to wrap table row if specific td class within it found

I've seen others who have used jQuery to add a class and I need to do the same based on id or class found within it.

Here is sample HTML

<tr>
<td>6</td>
<td>56</td>
<td>Speed Racer</td>
<td id="season2013">2013</td>
<td>14</td>
</tr>

I need to add a class or id tag to all tr when any of the td has in id of #season2013 I don't think what i have below will work , but was hoping for some help.

<script>
$(function(){
    $( '#season2013' ).wrap( '<tr class="newclass"></tr>' );
});
</script>

Upvotes: 1

Views: 109

Answers (2)

hunter
hunter

Reputation: 63512

Try using the has selector:

$("tr").has("#season2013").addClass("newclass");

You won't need to wrap the td in a new tr tag since it already has a tr parent. Instead add the class to that tr directly.

Upvotes: 0

j08691
j08691

Reputation: 207861

Try:

$(function(){
    $('#season2013').closest('tr').addClass('newclass');
});

Since your table already has <tr> tags, you don't need to wrap a new <tr>, just add the class to the <tr> that already exists.

Upvotes: 5

Related Questions