2

To make a long story short, I am setting up a simplecart(JS) and for multiple options I am using checkboxes. Everything works fine calculating the different options in the cart, exept I need to print the text "VAT 21% €90" or "World (Taxfree)" (depending on what has been checked) in #tax div. How can I do this?

 <script type="text/javascript">
simpleCart.tax = function(){
    if( $("#regionSelect").val() == "VAT 21% €90" ){return 90;}
    if( $("#regionSelect").val() == "World (Taxfree)" ){return 0;}
};
</script>


  <input type="checkbox" name="tax" id="regionSelect" class="item_tax" value="eu" onClick="">Eu VAT 21%
  <input type="checkbox" name="tax" id="regionSelect" class="item_tax" value="world"onClick="">World (Tax Free)   
  </dd>


<div id="tax"></div

UPDATE

Where exactly do I add this?

This only works for the later one.

    if( $("#regionSelect").val() == "eu" ){return 90;} $("#tax").html("VAT 21% €90")
    if( $("#regionSelect").val() == "world" ){return 0;} $("#tax").html("World (Taxfree)")
1
  • In reply to your update, no not quite. See my answer. You should have your html functions inside your if statements. Commented Sep 19, 2013 at 15:42

3 Answers 3

3

$("#regionSelect").val() will never equal VAT 21% ?90 or World (Taxfree) because you set the value of the input element in html to eu and world. What comes after the input of checkbox is not the value, just some random text that jquery will not read. Therefore, it should change to $("#regionSelect").val() == "eu" or $("#regionSelect").val() == "world" respectively.

Additionally, ids should be unique, and not duplicated. That's what classes are for. As mentioned above, the jQuery html() and text() functions are the way to go for printing text to elements.

Try this:

<script type="text/javascript">
simpleCart.tax = function(){
    if( $(".regionSelect").val() == "eu" ){return 90; $("#tax").html("EU VAT 21%");}
    if( $(".regionSelect").val() == "world" ){return 0; $("#tax").html("World (Taxfree)");}
};
</script>


<input type="checkbox" name="tax" class="item_tax regionSelect" value="eu" onClick="">Eu VAT 21%
<input type="checkbox" name="tax" class="item_tax regionSelect" value="world"onClick="">World (Tax Free)   
</dd>


<div id="tax"></div>

And also, you have a closing dd element? I'm assuming you also have an opening one as well. Otherwise that could also create problems.

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

3 Comments

Yes dd is in order.. thank you so much, I guess I have been awake to long to finish this one :)
uups my bad.. now the value of 0 or 90 is not added to the cart
This may be because I removed the id of regionSelect and made it a class. Your back end cart could be set to look for #regionSelect. You could also try putting the return statement after the $("#tax").html(...)
1

Make use of html function like as below.

$("#divid").html("World (Taxfree)")

Comments

0

First get the div you want to add the text to. And then add it with jquery's .text() function.

$("#divtoget").text("text to set")

Comments

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.