Skip to content Skip to sidebar Skip to footer

Hide Element If It Contains Specific Text

I have the following code on my ecommerce website. I would like to search to check whether it contains '0.00' If it does contain '0.00' I would like to hide the parent div contai

Solution 1:

If you can't change your HTML, it's a little tedious because the text must be cleand from the currencyCode and currencySymbol. A way to do this is to clone the div and then take the text :

$(".ct_pd_item_price.ct_pd_item_value").filter(function(){
   return $(this).clone().find('.ct_currencyCode, .ct_currencySymbol').remove().end().text().trim()=="0.00"
}).hide()

Demonstration

Of course a cleaner HTML, maybe with the value in a data attribute, would be easier to deal with.


Solution 2:

Another way with a regular expression

$('[itemprop="price"]').each(
    function() {
        var elem = $(this);
        if (elem.text().match(/[^\d]0\.00/)) {
            elem.closest(".ct_pd_item_price.ct_pd_item_value").hide();
        }
    }
);

Pulls out the text and it will be £0.00 so match that the price is not [anynumber]0.00


Post a Comment for "Hide Element If It Contains Specific Text"