views:

1086

answers:

3

Hello

I am trying to use Jquery to find TD's with X or Y 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');

None of which worked.

All help is appreciated

Sotkra

+8  A: 

You want to use :contains.

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

for the parent

$('td:contains("Dolares")').parent().addClass('highlight');
tvanfosson
@Sotkra: Do note `contains` looks for text (not html) and is case sensitive :)
o.k.w
I'd suggest looking at the linked documentation. If you need to differentiate various table elements that might contain the same string or need case insensitivity you could use filter() and supply a function that does a regex match on the text() of each element.
tvanfosson
Thanks for that tvan. Now, I was wondering how you'd do the opposite of the above, meaning, exclude (hide or hightlight, doesn't matter) rows which have a td with a value of X. As for the suggested filter(), I don't know how to do what you suggested. A suggestion by a friend, which works with case SENSITIVITY goes as follows: $('td').filter(function(){return $(this).html() == 'Dolares'}).parent('tr').addClass('highlight'); How would you transform it into case insensitive?
Sotkra
return $(this).text().match( /^\s*dolares\s*$/i ) != null; would do nicely to match only *dolares* in a case insensitive way. Use the hide()/show() function on the parent example to hide/show the row containing the string.
tvanfosson
What about the inverse though?"Thanks for that tvan. Now, I was wondering how you'd do the opposite of the above, meaning, exclude (hide or hightlight, doesn't matter) rows which have a td with a value of X."
Sotkra
$('td:not:contains("Dolares")') or simply reverse the sense of the comparison in the function.
tvanfosson
Yeah, silly me. I did that and it worked. It's nice to see you can also use :not:contains too. Thanks a lot TVAN.
Sotkra
+2  A: 

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.

o.k.w
A: 

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');
ScottD
None of these two examples worked. Did they work for you?
Sotkra