0

Hello i would like to hide a span and a div if the value is 0.00..here is my code

<span class="price-old"> <%=getCurrentAttribute('item','custitem_sumisho_listprice')%>      </span>
<span class="price-new"><%=getCurrentAttribute('item','custitem_sumisho_onlineprice')%>  </span>
<div class="save-sale" style="font-size: .8em; padding-top: 4em"><%=getCurrentAttribute('item','custitem_sumisho_totalsave')%></div>

In the above code if the div of class="save-sale" will have 0.00 value then i would like to hide the class="price-old" and class="save-sale".Here getattribute tag will get the value of item price.

I have tried the below jquery but its not working

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
$(function () {
if ($(".save-sale").text() == "0.00") {
$(".save-sale").hide();
$(".price-old").hide();
}
});
</script>
1

3 Answers 3

2

As you are having multiple save-sale div, you can use .each() to iterate them all and check its value for making save-sale and price-old hidden.

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
$(function () {
  $(".save-sale").each(function(){
  if ($(this).text().trim() == "0.00") {
       $(this).hide();
       $(this).prev(".prod-price").find(".price-old").hide();
    }
  });
});
</script>

DEMO

Sign up to request clarification or add additional context in comments.

12 Comments

I just suggested another way to achieve same, but problem may be in jquery include otherwise code looks good.
have you checked any console errors? and verified that jQuery library path is correct and getting loaded in browser?
do i need to convert it to rounded value?
You can use trim() as suggested by Sanki in below answer or share a jsfiddle link with your problem statement to help you better. Also these are ways to check if jquery loaded or not.
may be there are lots of items and it will check the value one by one so is it a problem for script not running?
|
0

You can use

$(".class or #id").hide()

or something similar but with used css style

$(".class or #id").css('display','none')

but if you want set this to all elements with class use obviously each function:

$(".class").each(function() { $(this).css('display','none') });

Comments

0

DEMO FIDDLE

Try this:

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
  $(function () {
    if ($.trim($(".save-sale").text()) == "0.00") {
    $(".save-sale").hide();
    $(".price-old").hide();
  }
});
</script>

For Multiple Divs with same classname you can try the following:

$(function () {
$(".save-sale").each(function (i) {
    if ($.trim($(this).text()) == "0.00") {
        $(this).closest(".save-sale").hide();
        $(this).closest(".price-old").hide();
    }
     else{
        $(this).closest(".save-sale").show();
        $(this).closest(".price-old").show();
    }
});
});

UPDATED FIDDLE

1 Comment

i have multiple divs with same class name how to apply this code on divs one by one to check values and hide classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.