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

    Question please check this code !!!!!

    I am trying to add values from 6 text boxes and display its sum automatically in 7th text box. This is not working. Please help.

    function calculate_a() {
    var a = document.getElementById('Para_A_A1_score').value ;
    var b = document.getElementById('Para_A_A2_score').value ;
    var c = document.getElementById('Para_A_A3_score').value ;
    var d = document.getElementById('Para_A_A4_score').value ;
    var e = document.getElementById('Para_A_A5_score').value ;
    var f = document.getElementById('Para_A_A6_score').value ;
    var g = (a+b+c+d+e+f) ;
    document.audit_billing_IE.product_name4.value = g ;

    Below is the html form code that should automatically display the total value without clicking on any submit or send button

    <td> Parameter A </td>
    <td colspan='3'> <input type="text" name="product_name4" id="product_name4" onchange="calculate_a(this);" /> </td>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    1) The score values as input by the user are strings, not numbers. You must convert them to numbers and also check that they are in fact numbers, with e.g.

    var a = Number(document.getElementById('Para_A_A1_score').value) || 0; // if a is not a number assign the value 0
    document.getElementById('Para_A_A1_score').value = a; // write the value back to the field

    2) Your onchange event handler makes no sense. You need to call the function calculate_a() onchange or onblur from each of the six textboxes, so that the sum total will update each time. Note that if the field is left blank the Number() method will convert that to 0.

    <input type = "text" id = "Para_A_A1_score" onchange = "calculate_a()">

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 08-29-2011 at 06:52 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


    Posting Permissions

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