user2067567
user2067567

Reputation: 3803

Traversing HTML using Jquery

My sample HTML

<tr>
    <td>
        <div><span id="temp" />
        </div>
    </td>
</tr>
<tr>
    <td>
        <div><span id="temp" />
        </div>
    </td>
</tr>

From the Span tag how do i get the Table tr tag and set its background color ? Any easy methods rather than finding parent.parent?

And only the particular span tag TR should be changed not all the table TR ?

Thanks

Upvotes: 1

Views: 106

Answers (3)

YD1m
YD1m

Reputation: 5895

Try .parents()

$('#temp').parents('tr').first().css({'background-color': '#fff'});

.first() exclude elements in cause of nested tables.

Or .closest()

$('#temp').closest('tr').css({'background-color': '#fff'});

Upvotes: 1

Piotr Uchman
Piotr Uchman

Reputation: 570

in jQuery:

$("#temp").parents("tr").css('background-color', 'green');              

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

you can use .closest()

$('#temp').closest('tr').css('background-color', 'green');

Upvotes: 11

Related Questions