user2639176
user2639176

Reputation: 91

Apply CSS to TR if certain style is in a TD

As title says, need to addclass() to a tr (the tr, if multiple) if the td has a certain class.

Example:

<table>
  <tr>
    <td>Test</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td class="scratched">Test</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Test</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td class="scratched">Test</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

So if the td has class "scratched", apply class "line" to the tr.

Upvotes: 0

Views: 258

Answers (3)

rink.attendant.6
rink.attendant.6

Reputation: 46307

Use the .has() filter:

$('tr').has('td.scratched').addClass('line');

See jsFiddle for example.

Upvotes: 3

GautamD31
GautamD31

Reputation: 28773

Try with .parent() like

$('td.scratched').parent('tr').addClass('line');

See the DEMO

Upvotes: 0

Erik A. Brandstadmoen
Erik A. Brandstadmoen

Reputation: 10608

Something along the lines of:

$('td.scratched').parent().addClass('line');

should work.

Upvotes: 2

Related Questions