views:

2556

answers:

4

I have 2 textbox in my asp.net page and also have one hiddenfield in my asp.net page , my hiddenfield will always have numeric value like 123.00 , and in my one textbox also I will always have numeric value like 20.00 now I want to add this hiddenfield value and textbox value and display it into second textbox thru javascript

I wrote the following code to do 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 result like 123.00+20.00 = 143.00 but this is concatnating hiddenamt value and fee value and giving me result like 12320.00 in my first textbox

can anybody suggest me what is wrong in my code and what is the right way to get desired value

+2  A: 

the value of an input is just a string - convert to float parseFloat(foo) in JS and you'll be fine

edited to make float as I notice it's probably important for you

annakata
A: 

Textboxes are strings, you need to convert from a String to a Number:

var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);

Eric

epascarello
+3  A: 
amt.value = parseFloat(hiddenamt) + parseFloat(fee);
Michael Haren
A: 

you should parse the values to decimals first:

decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;
Coentje
Poster is working in javascript--it looks to me that you're mixing C# and javascript.
Michael Haren
You are quite right. I missed the Javascript part, saw asp.net and forgot to read the rest.
Coentje