Hide all strings from a span matching criteria using Jquery

I am trying to hide all $0.00 strings from a page and tried but did not succeeded. I get error: TypeError: hidepricenull.each is not a function. I am using it in wordpress.

HTML

Read More
<span class="amount">£200.00</span>
<span class="amount">£200.00</span>

Jquery

jQuery(document).ready(function($) {
 var hidepricenull = $('span.amount').text();
     hidepricenull.each(function()   {
     if(hidepricenull == '£200.00') {
       $(this).hide(); 
     }
    });
});

Expected Output:

Hide the 2 text from spans

<span class="amount"></span>
<span class="amount"></span>

Related posts

Leave a Reply

4 comments

  1. Your hidepricenull value is a string (the first item’s text value, because of calling text()) but you need the collection and access the text per item:

    jQuery(document).ready(function ($) {
        var hidepricenull = $('span.amount');
        hidepricenull.each(function () {
            if ($(this).text() == '£200.00') {
                $(this).hide();
            }
        });
    });
    

    Note you could also start using the rather handy scoping DOM-ready shortcut for jQuery:

    jQuery(function ($) {
        var hidepricenull = $('span.amount');
        hidepricenull.each(function () {
            if ($(this).text() == '£200.00') {
                $(this).hide();
            }
        });
    });
    

    Note the temp variable is no longer required and you can use a filter instead:

    jQuery(function ($) {
        $('span.amount').filter(function () {
            return $(this).text() == '£200.00';
        }).hide();
    });
    

    Update to support multiple values:

    jQuery(function ($) {
        $('span.amount').filter(function () {
            var text = $(this).text();
            return text == '£200.00' || text == '£100.00';
        }).hide();
    });
    

    Update to support numeric comparisons (faster than strings for many values):

    jQuery(function ($) {
        $('span.amount').filter(function () {
            var value = parseFloat($(this).text().substring(1));
            return value == 200 || value == 100 || value == 50;
        }).hide();
    });
    
  2. You should iterate over the items and check for it’s text, you could try this:

    jQuery(document).ready(function($) {
        var hidepricenull = $('span.amount');
        hidepricenull.each(function(){
            if($(this).text() == '£200.00') {
                $(this).hide(); 
            }
        });
    });