Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 805
Capagris Avatar asked Dec 05 '22 04:12

Capagris


2 Answers

You want to use :contains.

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

for the parent

$('td:contains("Dolares")').parent().addClass('highlight');
like image 178
tvanfosson Avatar answered Jan 25 '23 16:01

tvanfosson


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.

like image 39
o.k.w Avatar answered Jan 25 '23 16:01

o.k.w