Highlight td if its value is one

I have a table like this.

         <table>
            <tr>
                <td>one</td>
                <td>two</td>
                <td>one two</td>
            </tr>
            <tr>
                <td>one</td>
                <td>two</td>
                <td>one two</td>
            </tr>
            <tr>
                <td>one</td>
                <td>two</td>
                <td>one two</td>
            </tr>
        </table>

and jquery code to highlight tdif its valueone

$("table > tbody > tr > td:contains('one')").css("background", "#6a5acd");

And the code will do td highligted if its value is equal one two. I want to highlight only td that haveone

+4
source share
3 answers

: contains is not suitable for this, as it just checks to see if a given line is present anywhere in the element, use the filter manual to do this

$("table > tbody > tr > td").filter(function(){
    return $.trim($(this).text()) == 'one'
}).css("background", "#6a5acd");

See the demo in fiddle

+4
source

try this one

 $("tr td:contains('one')").each(function(){
      $(this).css("background","#6a5acd");
 });

see demo in fiddle

0
source

contains . filter.

: , .

$("table > tbody > tr > td").filter(function() {
    return $(this).text() === "one"
}).css("background", "#6a5acd");

And here is jFiddle: http://jsfiddle.net/xDP9d/

0
source

Source: https://habr.com/ru/post/1524428/


All Articles