Results 1 to 9 of 9

Thread: Addition with JavaScript and Forms

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Addition with JavaScript and Forms

    I have a large number of forms on my webpage. How could I go about adding them up add displayin the result in another text box, as it's being typed?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This is the closest I've gotten, but it just concenates (or whatever that word is) them together, so 1, 2, and 3, it would display 123:

    Code:
    lengthy code removed...
    And what event should I call this with on the input area?
    Last edited by The Hobo; Apr 6th, 2002 at 10:43 AM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    In Java there's .intValue(), casting to an int, and int(some value), do those work?

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    No...unless I'm doing it wrong.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748
    You need to re-evaluate your parp directive

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Bonker Gudd
    You need to re-evaluate your parp directive
    Who?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Alright, I got it working, but is there anyway I could simplify this code, maybe using some kind of loop?

    Code:
    function count() {
      var a = parseInt(document.forms(0).box1.value) || 0;
      var b = parseInt(document.forms(0).box2.value) || 0;
      var c = parseInt(document.forms(0).box3.value) || 0;
      var d = parseInt(document.forms(0).box4.value) || 0;
      var e = parseInt(document.forms(0).box5.value) || 0;
      var f = parseInt(document.forms(0).box6.value) || 0;
      var g = parseInt(document.forms(0).box7.value) || 0;
      var h = parseInt(document.forms(0).box8.value) || 0;
      var i = parseInt(document.forms(0).box9.value) || 0;
      var j = parseInt(document.forms(0).box10.value) || 0;
      var k = parseInt(document.forms(0).box11.value) || 0;
      var l = parseInt(document.forms(0).box12.value) || 0;
      var m = parseInt(document.forms(0).box13.value) || 0;
    
      document.forms(0).total1.value = a + b + c + d + e + 
      f + g + h + i + j + k + l + m;
    
      var a = parseInt(document.forms(0).box14.value) || 0;
      var b = parseInt(document.forms(0).box15.value) || 0;
      var c = parseInt(document.forms(0).box16.value) || 0;
      var d = parseInt(document.forms(0).box17.value) || 0;
      var e = parseInt(document.forms(0).box18.value) || 0;
      var f = parseInt(document.forms(0).box19.value) || 0;
      var g = parseInt(document.forms(0).box20.value) || 0;
      var h = parseInt(document.forms(0).box21.value) || 0;
      var i = parseInt(document.forms(0).box22.value) || 0;
      var j = parseInt(document.forms(0).box23.value) || 0;
      var k = parseInt(document.forms(0).box24.value) || 0;
      var l = parseInt(document.forms(0).box25.value) || 0;
      var m = parseInt(document.forms(0).box26.value) || 0;
    
      document.forms(0).total2.value = a + b + c + d + e + 
      f + g + h + i + j + k + l + m;
    }
    box1 through box13 are added in total1 and box14 through box26 are added in total2.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Hi,

    Do you mean something like this...

    Code:
    function count(){
      tot1=0;
      for (i=1;i<14;i++){
    	  if (document.forms(0).item('box'+i).value==""){
    	    tot1=tot1+0;
    		}else{
    			tot1=tot1+parseInt(document.forms(0).item('box'+i).value);	
    		}
    	}
    	document.forms(0).total1.value=tot1;
    	tot2=0;
      for (i=14;i<27;i++){
    	  if (document.forms(0).item('box'+i).value==""){
    	    tot2=tot2+0;
    		}else{
    			tot2=tot2+parseInt(document.forms(0).item('box'+i).value);	
    		}
    	}
    	document.forms(0).total2.value=tot2;
    }
    Its a looped version of your code.
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That's great. Thanks alot
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width