xKevin
xKevin

Reputation: 53

jQuery addClass only to <td> where the parent <tr> has specific class

so what i try to do is, to add a class to every element, where the parent has a specific class.

I tried this:

if ($('tr').hasClass('storno')) {
        $('tr > td').addClass('storno');
    }

and this is the html example:

<table>
  <tr class="storno">
    <td>test</td>
    <td>test2</td>
  </tr>
  <tr>
    <td>test</td>
    <td>test2</td>
  </tr>
</table>

i tried it also with some other code, but at this point, i dont know i can get it right.

Thank you very much in advance

Upvotes: 5

Views: 6970

Answers (6)

user2549616
user2549616

Reputation:

Here, try this one

$('tr.storno').find('td').addClass('storno');

Upvotes: 1

Shivaji Ranaware
Shivaji Ranaware

Reputation: 169

if ($('tr').hasClass('storno')) {
    $(this).find('td').addClass('storno');
}

Upvotes: 0

BiAiB
BiAiB

Reputation: 14122

simply use a selector with the specific class when you fetch the nodes:

$('tr.specificParentClass').children('td').addClass('tdClass');

Upvotes: 1

revolver
revolver

Reputation: 2405

Do this

$('tr.storno > td').addClass('storno');

Upvotes: 1

mishik
mishik

Reputation: 10003

$('tr.storno > td').addClass('storno');

Upvotes: 12

Arun P Johny
Arun P Johny

Reputation: 388316

you need to use the class selector along with descendent selector to do this

$('tr.storno td').addClass('storno');

Demo: Fiddle

Upvotes: 7

Related Questions