Using jQuery to search for <td> content

I am trying to use jQuery to search for <td> with given content (text) inside. For instance:

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

From left to right, the 7th COLUMN, which reads "TIPO MONEDA", displays PESOS or DOLARES. I would like to be able to do the following:

  • Select <td> with the text "DOLARES" or "PESOS" and add a class to them.

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

For # 1, I tried with this:

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

as well as

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

None of them worked.

+4
source share
3 answers

You want to use :contains .

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

for parent

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

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

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

You can try contains("Dolares") , as tvanfosson suggested. This works for your case, as it is unlikely that you will have "xxDolaresxx" in other TDs.

+2
source

You are 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'); 
-one
source

All Articles