Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    115
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Adding 2 Elements Together

    How can I add 2 elements together? I have ..

    PHP Code:
    <script>
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6').value Number(getElementById('call_4').value) +  Number(getElementById('call_5').value);
    };
    </script> 
    I also tried

    PHP Code:
    <script>
    document.getElementById('do').onclick = function() {
    Number(getElementById('call_6').value) = Number(getElementById('call_4').value) +  Number(getElementById('call_5').value);
    };
    </script> 
    I had it as just document.getElementById before but it just combined the numbers (ex: 1 + 2 = 12 instead of 3). With document.getElementById instead of Number I can multiply as well.

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Try wrapping parseInt() around each item that is going to be added

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This hasn't a thing to do with PHP. Moving to Javascript forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by 7daytheory View Post
    How can I add 2 elements together? I have ..

    PHP Code:
    <script>
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6').value Number(getElementById('call_4').value) +  Number(getElementById('call_5').value);
    };
    </script> 
    I don't know why you missed "document." when you didn't in others?
    Code:
    document.getElementById('do').onclick = function() {
        document.getElementById('call_6').value = Number(document.getElementById('call_4').value) +  Number(document.getElementById('call_5').value);
    };

  • #5
    Regular Coder
    Join Date
    Aug 2013
    Posts
    115
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Sorry for some reason I thought I was posting in JS forum, must have had 2 tabs up.

    I tried both of your suggestions and it still didn't work, i dont know why such a simple thing is causing me such a problem.

    I tried..

    Code:
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6') = parseInt(Number(document.getElementById('call_4').value)) + parseInt(Number(document.getElementById('call_5').value));
    };
    Code:
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6') = parseInt(Number(getElementById('call_4').value)) + parseInt(Number(getElementById('call_5').value));
    };
    Code:
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6') = parseInt(document.getElementById('call_4')) + parseInt(document.getElementById('call_5'));
    };
    Code:
    document.getElementById('do').onclick = function() {
    document.getElementById('call_6') = parseInt(document.getElementById('call_4').value) + parseInt(document.getElementById('call_5').value);
    };
    Nothing yet.

    Any other ideas?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    replace

    Code:
    document.getElementById('call_6') =

    with


    Code:
    document.getElementById('call_6').value =
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You don't have to use parseInt() and Number() together. Both return a number but they have differences. One is parseInt("2blahblah") returns 2, while Number("2blahblah") returns NaN (Not a Number). I think you need to use Number() in your case.

    I think your problem is you might be setting onclick when the element has not rendered yet? Where did you put the script? You need to put it in window.onload handler or better, right before the </body> tag.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    And you also need to short-circuit the sum with 0 to avoid NaN total.
    PHP Code:
    document.getElementById('do').onclick = function() {
        
    document.getElementById('call_6').value =  ( Number(document.getElementById('call_4').value) +  Number(document.getElementById('call_5').value) ) || 0;
    }; 

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by glenngv View Post
    And you also need to short-circuit the sum with 0 to avoid NaN total.
    "can", not "need". As a mathematician I'd actually argue that adding nothing to nothing is indeed not a number. It certainly is not the same as adding zero and zero.

    Providing 0 as a defalt value is at most a kind thing to do because "NaN" may not look nice. But it is nothing other than error handling.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Then I guess defaulting to empty is better.

    PHP Code:
    document.getElementById('call_6').value =  ( Number(document.getElementById('call_4').value) +  Number(document.getElementById('call_5').value) ) || ""

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    Then I guess defaulting to empty is better.

    PHP Code:
    document.getElementById('call_6').value =  ( Number(document.getElementById('call_4').value) +  Number(document.getElementById('call_5').value) ) || ""
    wouldn't that replace zero with "" when adding, for example, -3 and 3 ?

    often times, numbers should legitimately add up to zero, like when balancing transactions...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,961
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by rnd me View Post
    wouldn't that replace zero with "" when adding, for example, -3 and 3 ?
    You're right.


  •  

    Posting Permissions

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