Capagris
Capagris

Reputation: 3841

Using jQuery to find the content of a <td>

I am trying to use jQuery to find <td>s with given content (text) inside. For example:

http://sotkra.com/btol/index.php

From left to right, the 7th COLUMN that reads 'TIPO MONEDA' displays either PESOS or DOLARES. I'd like to be able to do the following:

  1. Select <td>s with the text 'DOLARES' or 'PESOS' and add a class to them.

  2. Select <td>s with the text 'DOLARES' or 'PESOS' and add a class to their parent <tr>s.

For #1, I tried with this:

$('td[innerHTML=Dolares]').addClass('highlight');

and also

$('td[html=Dolares]').addClass('highlight');

Neither of which worked.

Upvotes: 4

Views: 6004

Answers (3)

ScottD
ScottD

Reputation: 304

You're very close, just missing some ticks around the value in your selector. This should work:

$("td[innerHTML='Dolares']").addClass('highlight');

And, for the parent selector:

$("td[innerHTML='Dolares'][innerHTML='Pesos']").parent().addClass('someClass');

Upvotes: -1

o.k.w
o.k.w

Reputation: 25810

Looking at the jQuery selector reference, I don't think
$('td[innerHTML=Dolares]') and
$('td[html=Dolares]') is going to work.

I can only think of iterating through all the TDs and check the $(tdselector).html() content.

You can try contains("Dolares") as suggested by tvanfosson though. It works for your case as it's highly unlikely you will have "xxDolaresxx" in other TDs.

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532465

You want to use :contains.

$('td:contains("Dolares")').addClass('highlight');

for the parent

$('td:contains("Dolares")').parent().addClass('highlight');

Upvotes: 15

Related Questions