SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [object HTMLInputElement] shows instead of total

    Hi guys, another lame question, Im sorry, but Ive been playing with this for a while and cant work out why this is happening.

    I am trying to add some variables and display their contents in an input field. My code looks something like this:
    Code:
    var Num1 = document.getElementById('1stTotal');
    var Num2 = document.getElementById('2ndTotal');
    var Num3 = document.getElementById('3rdTotal');
    var Num4 = document.getElementById('4thTotal');
    var Num5 = document.getElementById('5thTotal');
    				
    var Total = Num1 + Num2 + Num3 + Num4 + Num5;
    				
    grandTotal.value = Total;
    Now, this gives me [object HTMLInputElement] in the text field instead of the value. How do I need to structure this in order for it to work?

    thanks again!
    International calls from the UK
    Cheap International Calls

  2. #2
    SitePoint Member Roj's Avatar
    Join Date
    Jul 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Use value property of an object.

    e.g.

    Code:
    var Num1 = document.getElementById('1stTotal').value;
    var Num2 = document.getElementById('2ndTotal').value;
    var Num3 = document.getElementById('3rdTotal').value;
    var Num4 = document.getElementById('4thTotal').value;
    var Num5 = document.getElementById('5thTotal').value;
    				
    var Total = Num1 + Num2 + Num3 + Num4 + Num5;
    				
    grandTotal.value = Total;
    Thank You
    Roj Kumar Shahi
    Web Developer from Nepal

  3. #3
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply, but that just joins the strings together instead of adding the values and returning the sum.

    Can anyone else help?

    thanks
    International calls from the UK
    Cheap International Calls

  4. #4
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Warszawa
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then wrap all NumX into parseInt() function.

  5. #5
    SitePoint Addict JamieJelly's Avatar
    Join Date
    Jan 2004
    Location
    London
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    parseInt() didnt work but after digging around I found the Number() function which did the trick.

    thanks
    International calls from the UK
    Cheap International Calls


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
  •