0

When someone clicks my price up button, I want the price to increase by .1 in the #priceInput box. This is just not working though.. what am I doing wrong?

$("#priceUp").click(function(){ 
    var xyz = parseInt($("#priceInput").val());
    xyz=(xyz+.1);
    $("#priceInput").val(xyz);
});
2
  • 4
    elaborate on "just not working" Commented Jan 7, 2012 at 17:49
  • What's the outcome? is the value changing? nothing happens? did you make sure the event fires? Commented Jan 7, 2012 at 17:50

3 Answers 3

5

You are trying to parse a decimal number to an integer, so your solution can't work because the mathematic evaluator round your number.
If you run parseInt(0.1), the result is 0.

So, use parseFloat instead of parseInt.

You need also a little trick to avoid the '0.30000000000000004' "bug" (it's not really a bug).
Solution here : http://jsfiddle.net/erLqa/

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

2 Comments

Your answer tells OP how to fix it, not what he is doing wrong.
I've added a little explanation.
4

It may not work because you add 0.1, but later you use a parseInt. Use parseFloat instead of parseInt.

Comments

2

You probably want to use parseFloat, not parseInt since you're increasing the price by 0.1. Also, you need to make sure that the value of #priceInput is a number:

$("#priceUp").click(function() {
    var xyz = parseFloat($("#priceInput").val() || 0);
    xyz = (xyz + .1);
    $("#priceInput").val(xyz);
});

Here's a working example.

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.