Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate average of numbers in X qty. of textboxes

Status
Not open for further replies.

Muddsnapper

Programmer
Apr 24, 2007
2
US
What I want to do is update a text box with the current average of score ratings as a user enters score (numbers into text boxes) ratings for a series of questions.

I have been trying to figure this out for a couple days using different JavaScript's that I've copied but keep running into "gotchas".

So in desperation I've pieced together someone else's JavaScript (seems the most condensed of all I've looked at) with my Form. But since I don't know JavaScript I can't tie them in together. Can anyone give me a clue? Please...

Code:
<script language="JavaScript">

<!-- Another interesting Average routine I came across and worth mentioning is depicted in the code below which loops through n textboxes

// Code Start 
var Sum = 0; 
var DivideBy = 0; 
for (i=0; i<=9; i++) { 
if (getField("n."+i).value != "") { 
Sum += getField("n."+i).value; 
DivideBy++; 
} 
} 
getField("Result").value = Sum/DivideBy; 


// Code End ..submitted by Doron Shefer (Google Groups).

//-->

</script> 



</head>

<body>

<form action="Script_Name.htm" name="surveyForm">
<table border="1">
  <tr>
    <td>Running Average</td>
    <td>&nbsp;
<input name="totalBox" type="text"  onfocus="this.blur()"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question01" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question02" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question03" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question04" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question05" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question06" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question07" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question08" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question09" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;
<input type="text" name="question10" size="3" onblur="average (this)"></td>
  </tr>
  <tr>
    <td>Type a rating number between 0 and 100</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
  	<td  colspan="2" bgcolor="#FFffff" ><input type="submit" name="submit" value="Submit">              
	</td>
  </tr>
</table>
        </form>
</body>
</html>
 
Ok, if you see you have a function call on all the textboxes on the onblur event.

The function is called average and it's passing a reference to "this", which is the item it's being called from, the input textbox object.

You do want to call a function on the onblur, but you don't need to pass anything with the function, unless you have like 1000 textboxes.

Your first step is to set up the function called average.

I'd go about it by doing a getElementsByTagName("input") and loop through each <input> item in a for loop, checking the substring of the name of the input. If the first 5 letters are "quest" then see if the textbox is not blank and contains a valid number. If it does, the add 1 to a number total and add that value to a total. Do this for each textbox then divide by the number of valid textbox values and put that value into the total textbox.

Try one thing at a time, if you have questions, feel free to ask.



[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top