SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Sipovo
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculate while I type

    Hello,
    I believe that this has been answered somewhere but I cannot find it. I have 51 textboxes on page. In 50 I can enter value. 51st should tell me what is average of entered numbers.

    I need to sum values from fields that have value (not emty fields) and to divide this number by number of non empty fields. This should occur on every change in any of those 50 textboxes so to have live preview of average.

    I'm really new to JS and I don't know even how to sum these numbers.

    Thanks for response.

  2. #2
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is real quick & dirty, but it should get you started. Since you indicated a fixed number of input boxes, I went this route. If you setup the page for 50 inputs, you'll need to change the condition of the FOR loop to "x<51".

    I hope this helps!

    Code:
    <script type="text/javascript" language="javascript">
    function calculate(){
    	
    	var counter = 0;
    	var total = 0;
    	
    	for(x=1;x<16;x++){
    		if(!isNaN(parseInt(document.getElementById(x).value))){
    			total = total + parseInt(document.getElementById(x).value);
    			counter++
    		}
    	}
    	
    	if(counter > 0){
    		document.getElementById('average').value = "" +	(total/counter);
    	}
    
    }
    </script>
    </head>
    <body>
    <input type="text" name="textfield" id="1" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="2" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="3" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="4" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="5" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="6" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="7" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="8" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="9" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="10" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="11" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="12" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="13" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="14" onKeyUp="calculate();">
    <br>
    <input type="text" name="textfield" id="15" onKeyUp="calculate();">
    <br>
    <br>
    <input type="text" name="average" id="average">


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
  •