JQuery contains strings

I want to add a class to a specific line, but the problem is that the line starts with number 0.

This is my code that I want to add only to the "0 BAM" class, but when I add this jQuery, it adds the class to all divs that have 0 and BAM, Like the first "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');

Thanks!

+4
source share
1 answer

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

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

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


All Articles