Edin Puzic
Edin Puzic

Reputation: 1048

Jquery contains strings

I whant to add class on specific string but problem is that string starts with number 0.

This is my code I whant to add just on "0 BAM" class but when I add this Jquery he add class on all divs that have 0 and BAM, Like first one "290 BAM"

<div class="views-field-commerce-price"> 290  BAM</div>
<div class="views-field-commerce-price"> 0  BAM</div>
<div class="views-field-commerce-price"> 223  BAM</div>
<div class="views-field-commerce-price"> 490  BAM</div>

  $('.views-field-commerce-price:contains("0  BAM")').addClass('item-3');

Thank You!

Upvotes: 4

Views: 163

Answers (1)

Satpal
Satpal

Reputation: 133433

I think you need exact comparison, Here you can use .filter()

$('.views-field-commerce-price').filter(function(){
    return $(this).text().trim() == "0  BAM";
}).addClass('item-3');

Upvotes: 13

Related Questions