Frage

I want to use the jQuery contains selector only for a table with a unique id. I tried it like this but doesnt work.

var x = "tree";
$( "#TableID.tr:contains('" + x + "')").css( "background-color", "red" );

The code should change the backgroundcolor of every row which contains x in the tr tag in a table with the id TableID.

Can anyone help me with the correct syntax?

War es hilfreich?

Lösung

you should use descendant selector for the table and the tr element

$( "#TableID tr:contains('" + x + "')").css( "background-color", "red" );

Your code looks for a table with id TableID with class tr and contains the given text some where within it

Andere Tipps

You need to separte the .tr form the table id and remove the dot as it is not a class element.

$( "#TableID tr:contains('" + x + "')").css( "background-color", "red" );

Living demo

Try this,

$( "tr:contains('" + x + "')").css( "background-color", "red" );

Demo link http://jsfiddle.net/dhana36/YT3CD/2/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top