Results 1 to 3 of 3

Thread: Javascript to sum variables

  1. #1
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript to sum variables

    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

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,799
    Thanks
    2
    Thanked 420 Times in 414 Posts

    Default

    Code:
    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.
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Got it;

    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •