0

Ok, I have these input boxes.

<input 
   type="text" 
   name="amount" 
   class="validate[required] text-input" 
   id="amount">

<input type="text" 
       name="incfee" 
       id="incfee" 
       readonly>

And this is the javascript I have :-

  <script type="text/javascript">
     window.onload = function() {
        var amount = document.getElementById('amount');
        var incfee = document.getElementById('incfee');

        var fee = 0.01;

        amount.onkeyup = function() {
           var result = parseFloat(amount.value) * fee;
           // var result1 = result + amount;
           incfee.value = !isNaN(result) ? result : '';
        };
     }
  </script>

Now, the problem is, that if I comment the line "var result1 = result + amount;" and rename result1 to result in incfee.value , the value of the textbox (amount including fee) changes with the value in amount and everything works fine. BUT, If I uncomment the line var result1 = result + amount; and change result to result 1 in incfee.value, the javascript doesn't works and no value is populated in the incfee textbox. What mistake am I doing? Thanks.

2
  • Check for errors in your browser's dev tools. Commented Jan 17, 2014 at 16:07
  • @J0e3gan This would most likely not result in an error. Commented Jan 17, 2014 at 16:10

2 Answers 2

8

var result1 = result + parseFloat(amount.value);

Javacript doesnt know how to add a float and an input-object.

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

6 Comments

It actually does, but the result might be surprising.
Don't forget to convert that value, or you're gonna get a string concatenation.
I like learning about math with non-numbers. I'm curious to know what a float + HTMLInputElement equals.
Might want to simplify those two lines to var result1 = amount.value * (1 + fee); and skip the intermediate step (and all the parseFloats.
@Brandon - let's say the result equaled 0.01 . . . the "float + HTMLInputElement" computation would net the following string: "0.01[object HTMLInputElement]"
|
1

Use .value to get value of input text

var amount = document.getElementById('amount').value; 
var incfee = document.getElementById('incfee').value;

1 Comment

While you've got the right idea, simply modifying these two lines as you've shown would just further break his code.

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.