SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple addition using integers stored in variables...not working

    Hi All,

    this must be really simple but i cant get it to work and it seems so simple that i cant find any website on why this doesnt work for me.

    so i have this simple function...

    function update(value, oldvalue)
    {
    var a = value;
    var b = oldvalue;
    var result = a + b;
    document.form.fieldname.value=result;
    }

    and call the function using a field with this...

    onkeyup="update(this.value, other)"

    But all its doing is concatenating the 2 numbers together as though they are strings!!!

    so if this.value = 11
    and other = 15
    it displays 1115

    i want to add them and display the total, do i need to specify them as being integers or something?

    Any help would be greatly appreciated!

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Values are stored as strings in the value property. You need to cast it to a Number
    Code:
    function update(value, oldvalue) {
      document.form.fieldname.value = parseInt(value) + parseInt(oldvalue);
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pepejeria,

    Thanks for the solution their, i have applied it and its working perfectly now!

    Regards

    Arc


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
  •