2

I'm trying to multiply and input amount by 3.5%, Can anyone give me any ideas how to do it?

$("#invest_amount").keyup(function() {

         $('#fee').val($('#invest_amount').val() + 3.5);

        });
1
  • 2
    You write in your question that you want to multiply, yet you write + in your code instead of multiplication. Commented Dec 8, 2010 at 10:26

4 Answers 4

4

You need to parse the input first then use * to multiply, not +, for example:

$("#invest_amount").keyup(function() {
  $('#fee').val(parseFloat($('#invest_amount').val()) * .035);
});

or...

$("#invest_amount").keyup(function() {
  $('#fee').val(+$('#invest_amount').val() * .035);
});

.val() returns a string, so you need to use + or praseFloat() to convert it to a number first

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

3 Comments

Do you know how I can use .toFixed(2)with this? Its not working for me?
@user534830 - like this: $('#fee').val((+$('#invest_amount').val() * .035).toFixed(2));
If you're multiplying, there's actually no need to force the coercion with + because * will do it for you, e.g. $('#fee').val(($('#invest_amount').val() * .035).toFixed(2));
3

You need to parse the value first:

var value = parseFloat($('#invest_amount').val());
if (!isNaN(value)) {
    $('#fee').val(value * 0.035);
} else {
    alert('The entered value is not a valid number');
}

Comments

0
$( '#fee' ).val( $( '#invest_amount' ).val() * .035 );

Comments

0

Knowing mathematics, and the significance of mathematical symbols, seems to be a basic prerequisite for your problem. Read more about mathematics here: http://en.wikipedia.org/wiki/Mathematics

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.