PDA

View Full Version : Javascript to sum variables



Simora
09-04-2009, 08:48 AM
I am trying to add ( SUM) a input value to a random number.
I want to display the SUM of these 2 values only after the input field is filled out, along with an instruction note that ask them to enter something in another input field.

Here's where I cant seem to get a sum.

[CODE]
var total = document.Addition.randomnumber.value + document.Addition.userInput.value;

document.getElementById('myDiv').innerHTML = total;
[CODE]

Your total is: <span id='myDiv'>

How can I get 'myDiv' to show the total. Its Concatenating rather than summing.

Code samples welcome.

Thanks

vwphillips
09-04-2009, 09:21 AM
var total = document.Addition.randomnumber.value*1 + document.Addition.userInput.value*1;


input values as 'type' string, assuming the value is a number multiplying by 1 changes the 'type' to a number.

Simora
09-05-2009, 09:59 PM
Got it;

Thanks