Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 774
KevinXVX Avatar asked Jul 11 '13 10:07

KevinXVX


2 Answers

$('tr.storno > td').addClass('storno');
like image 158
mishik Avatar answered Nov 15 '22 06:11

mishik


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

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

Demo: Fiddle

like image 43
Arun P Johny Avatar answered Nov 15 '22 06:11

Arun P Johny