RBS
RBS

Reputation: 3851

how to add 2 values of textbox in asp.net using javascript

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

Upvotes: 1

Views: 9133

Answers (4)

Coentje
Coentje

Reputation: 520

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;

Upvotes: -2

Michael Haren
Michael Haren

Reputation: 108246

amt.value = parseFloat(hiddenamt) + parseFloat(fee);

Upvotes: 3

epascarello
epascarello

Reputation: 207501

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

Upvotes: 0

annakata
annakata

Reputation: 75794

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

Upvotes: 2

Related Questions