Xeptor
Xeptor

Reputation: 477

Need to replace string with jQuery by exact match

I'm aware there's a few answers out there, but I can't get it to work properly.

I'm using a little script to change a few strings - but now I need to change a "0" and it wont work since the div I'm targeting also contains larger numbers. How do I do this on the exact match only, in this case a zero?

The script I'm using:

$( "td.stockinfo:contains('0')" ).text('*').addClass( "outofstock" );

Upvotes: 1

Views: 386

Answers (2)

Satpal
Satpal

Reputation: 133403

You can use $.fn.filter()

Reduce the set of matched elements to those that match the selector or pass the function's test.

Script

$("td.stockinfo").filter(function() {
    return $.trim($(this).text()) == '0';
}).text('*').addClass("outofstock");

Upvotes: 5

Tushar
Tushar

Reputation: 87203

You can use each.

Try this:

$("td.stockinfo").each(function() {
    if ($.trim($(this).text()) == '0') {
        $(this).text('*').addClass("outofstock");
    }
});

Upvotes: 0

Related Questions