Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By Ronald Roe

Thread: multiply input values and display in another input field in javascript

  1. #1
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917

    multiply input values and display in another input field in javascript

    I need to add input field and display in another input field.
    HTML Code:
    <tr>
    <td><input type="text" id="a1" name="a[1]"></td>
    <td><input type="text" id="b1" name="b[1]"></td>
    <td><input type="text" id="total1" name="total[1]"></td>
    </tr>
    <tr>
    <td><input type="text" id="a2" name="a[2]"></td>
    <td><input type="text" id="b2" name="b[2]"></td>
    <td><input type="text" id="total2" name="total[2]"></td>
    </tr>
    <tr>
    <td><input type="text" id="a3" name="a[3]"></td>
    <td><input type="text" id="b3" name="b[3]"></td>
    <td><input type="text" id="total3" name="total[3]"></td>
    </tr>
    HTML Code:
    <script type="text/javascript">
    	function change() {
    		for(i = 0; i < 3; i++){
    		var num1 = document.getElementById('a'+i).value; 
    	    var num2 = document.getElementById('b'+i).value;
    	    var total = document.getElementById('total'+i);
    	    var myResult = num1 * num2;
    	    total.value = myResult;	
    	 }
    }
    </script>
    I am able to add them and display it in another field using above code without using loop but I want to add loop for unlimited rows. Can anyone please help me?

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    You'll want to do something more like:

    HTML Code:
    <form>
      <input type="text" id="a">
      <input type="text" id="b">
      <input type="text" id="total">
    </form>
    
    <form>
      <input type="text" id="a">
      <input type="text" id="b">
      <input type="text" id="total">
    </form>
    
    <form>
      <input type="text" id="a">
      <input type="text" id="b">
      <input type="text" id="total">
    </form>
    HTML Code:
    <script>
    function calc(){
      var forms = document.forms;
      for(var i = 0; i < forms.length; i++){
        var a = forms[i].querySelector('#a').value;
        var b = forms[i].querySelector('#b').value;
        forms[i].querySelector('#total').value = a * b;
      }
    }
    </script>
    Now, you can add as many rows of them as you want and it'll calculate all of them.
    http://codepen.io/ronaldroe/pen/adpJVQ
    Last edited by Ronald Roe; Jan 02nd, 2016 at 01:02 PM.
    TheGAME1264 and leo037 like this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917
    Thanks a lot for this solution. It worked but there was problem while submitting the form as the form gets nested. I guess the browser assumes form closes at the first form, so only the first form data is inserted into mysql.

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    You never mentioned anything about databases. Each input will require a unique name attribute for that.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  6. #5
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917
    I've added unique name.

  7. #6
    Junior Member
    Join Date
    Jan 2016
    Posts
    13
    Member #
    52917

    solved.

    I was able to solve the problem. I changed i=0 to i=1 in my first code and it worked.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:00 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com