How to add 2 textbox values in asp.net using javascript
I have 2 textboxes in my asp.net page and also have one hidden field in my asp.net page, my hidden field will always have a numeric value like 123.00 and my textbox will always have a numeric value too. eg 20.00 I want to add this hidden field value and textbox and display it in the second textbox via javascript
I wrote the following code for this
var amt = document.getElementById("txtsecond");
var hiddenamt = document.getElementById("AmtHidden").value
var fee = document.getElementById("txtFirst").value;
amt.value = hiddenamt + fee;
this should give me a result, for example 123.00 + 20.00 = 143.00, but that concatenates the value of the hidden cost and cost and gives me a result like 12320.00 in my first text box.
can someone tell me what is wrong in my code and what is the correct way to get the desired value
amt.value = parseFloat(hiddenamt) + parseFloat(fee);
the input value is just a string - convert to float parseFloat(foo)
in JS and you will be fine
edited to make a float as I notice this is probably important to you
Text boxes are strings, you need to convert from string to number:
var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);
Eric
first you need to parse the values to decimal places:
decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;