user1963937
user1963937

Reputation: 205

Find parent for child value in DOM

I have this html-table:

<table id=mytable>
<tr>
<td>
Mark
</td>
<td>
Susan
</td>
</tr>
</table>

Somewhere from javascript an event occur and I can will be able to fetch the name, which is one of those in the table. From javascript/jquery I need to find the td-element containing the name and color it. I tried with:

$("#mytable").find("td:contains('Mark')").parent().css('background-color', 'red'); 

But the td-element doesnt get colored.

Upvotes: 1

Views: 67

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

I believe you need to try this:

$("#mytable").find("td:contains('Mark')").css('background-color', 'red');

DEMO

OR

$("#mytable td:contains('Mark')").css('background-color', 'red');

DEMO

Upvotes: 1

mohamed-ibrahim
mohamed-ibrahim

Reputation: 11137

You already selected the td what you did is you tried to add a color to the tr which is the parent of the selected td:

$("#mytable").find("td:contains('Mark')").css('background-color', 'red'); 

that's why you example should be highlighting both td as the color applied for the tr

Upvotes: 0

Related Questions