javascript help

Experts,

I'm trying to use an onchange event handler to call the calcAvg function which passes five parameters to the performcalc function so it can average the five numbers and return it back to the calcavg function; this in turn displays the result in the final text box txtresult.

what am i doing wrong (or have i done anything correctly?)

Thanking you in advance for the help,

Dan
<script type="text/javascript">
/* <![cdata[*/

function calcAvg(t1,t2,t3,t4,t5)
{
	var textinfo = document.newInput.value;
	return textinfo;
}

function performCalc(a,b,c,d,e)
{
	var sum = a+b+c+d+e;
	var calcResult = sum / 5;
	return calcResult;
}


/* ]]> */
</script>


</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="351" border="5" cellpadding="5" cellspacing="5">
    <tr>
      <td><label for="txt1">Number 1</label>
      <input type="text" name="txt1" id="txt1" tabindex="1" value="0" onchange="var newInput = calcAvg(
      document.newInput.value);"  /></td>
    </tr>
    <tr>
      <td><label for="txt2">Number 2</label>
      <input type="text" name="txt2" id="txt2" tabindex="2" value="0" onchange="var newInput = calcAvg(
      document.newInput.value);"/></td>
    </tr>
    <tr>
      <td><label for="txt3">Number 3</label>
      <input type="text" name="txt3" id="txt3" tabindex="3" value="0" onchange="var newInput = calcAvg(
      document.newInput.value);"/></td>
    </tr>
    <tr>
      <td><label for="txt4">Number 4</label>
      <input type="text" name="txt4" id="txt4" tabindex="4" value="0" onchange="var newInput = calcAvg(
      document.newInput.value);"/></td>
    </tr>
    <tr>
      <td><label for="txt5">Number 5</label>
      <input type="text" name="txt5" id="txt5" tabindex="5" value="0" onchange="var newInput = calcAvg(
      document.newInput.value);"/></td>
    </tr>
    <tr>
      <td><label for="txtResult">averageResult</label>
      <input type="text" name="txtResult" id="txtResult" tabindex="6"onchange="var newInput = performCalc();" /></td>
    </tr>
  </table>
</form>
</body>

Open in new window

dvcrduAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
See if you can learn something from this
<html>
<head>
<script type="text/javascript">
/* <![cdata[*/

function calcAvg(a,b,c,d,e)
{
	var n1 = parseInt(a);
	var n2 = parseInt(b);
	var n3 = parseInt(c);
	var n4 = parseInt(d);
	var n5 = parseInt(e);
	var sum = n1+n2+n3+n4+n5;
	var calcResult = sum / 5;
	return calcResult;
}

function performCalc()
{
	var calcResult = calcAvg(document.form1.txt1.value, document.form1.txt2.value,
		document.form1.txt3.value, document.form1.txt4.value, document.form1.txt5.value);
	document.form1.txtResult.value = "" + calcResult;
}

/* ]]> */
</script>


</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="351" border="5" cellpadding="5" cellspacing="5">
    <tr>
      <td><label for="txt1">Number 1</label>
      <input type="text" name="txt1" id="txt1" tabindex="1" value="0" onchange="performCalc();"  /></td>
    </tr>
    <tr>
      <td><label for="txt2">Number 2</label>
      <input type="text" name="txt2" id="txt2" tabindex="2" value="0" onchange="performCalc();"/></td>
    </tr>
    <tr>
      <td><label for="txt3">Number 3</label>
      <input type="text" name="txt3" id="txt3" tabindex="3" value="0" onchange="performCalc();"/></td>
    </tr>
    <tr>
      <td><label for="txt4">Number 4</label>
      <input type="text" name="txt4" id="txt4" tabindex="4" value="0" onchange="performCalc();"/></td>
    </tr>
    <tr>
      <td><label for="txt5">Number 5</label>
      <input type="text" name="txt5" id="txt5" tabindex="5" value="0" onchange="performCalc();"/></td>
    </tr>
    <tr>
      <td><label for="txtResult">averageResult</label>
      <input type="text" name="txtResult" id="txtResult" tabindex="6" /></td>
    </tr>
  </table>
</form>
</body>

Open in new window

0
 
leakim971PluritechnicianCommented:
you're passing only one parameter
you should use onkeyup or "better" onblur (when you left the field) instead onchange
0
 
dvcrduAuthor Commented:
i'm being required to use onchange, unfortunately.

are you saying i need to have only one parameter with the first function?

i'll also need to use the parsint function to make sure that everything is calculated as numbers; how would i encorporate that ?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
leakim971PluritechnicianCommented:
>i'm being required to use onchange

on an input element ? to do what ?
0
 
dvcrduAuthor Commented:
i need to add an onchange event handler to each text box (5 textboxes total) that calls the calcavg function and passes the value of that text box by referencing its document object, form name, and name and value attributes.

the calcAvg function then passes the five parameters to the performcalc function so it can average the five numbers and return it back to the calcavg function; this in turn displays the result in the final text box txtresult.

i'll also need to use the parsint function to make sure that everything is calculated as numbers
0
 
leakim971Connect With a Mentor PluritechnicianCommented:
Currently : function calcAvg(t1,t2,t3,t4,t5) // five parameter
You call : calcAvg(document.newInput.value); // only one parameter

check this :




<script type="text/javascript">
/* <![cdata[*/

function calcAvg()
{
	a = parseInt(document.getElementById("txt1").value.replace(/\D/g, ""));
	b = parseInt(document.getElementById("txt2").value.replace(/\D/g, ""));
	c = parseInt(document.getElementById("txt3").value.replace(/\D/g, ""));
	d = parseInt(document.getElementById("txt4").value.replace(/\D/g, ""));
	e = parseInt(document.getElementById("txt5").value.replace(/\D/g, ""));
	var sum = isNaN(a)?0:a + isNaN(b)?0:b + isNaN(c)?0:c + isNaN(d)?0:d + isNaN(e)?0:e;
	var calcResult = sum / 5;
	document.getElementById("txtResult").value = calcResult;
}


/* ]]> */
</script>


</head>

<body onload="calcAvg()">
<form id="form1" name="form1" method="post" action="">
  <table width="351" border="5" cellpadding="5" cellspacing="5">
    <tr>
      <td><label for="txt1">Number 1</label>
      <input type="text" name="txt1" id="txt1" tabindex="1" value="0" onchange="calcAvg();"  /></td>
    </tr>
    <tr>
      <td><label for="txt2">Number 2</label>
      <input type="text" name="txt2" id="txt2" tabindex="2" value="0" onchange="calcAvg();"/></td>
    </tr>
    <tr>
      <td><label for="txt3">Number 3</label>
      <input type="text" name="txt3" id="txt3" tabindex="3" value="0" onchange="calcAvg();"/></td>
    </tr>
    <tr>
      <td><label for="txt4">Number 4</label>
      <input type="text" name="txt4" id="txt4" tabindex="4" value="0" onchange="calcAvg();"/></td>
    </tr>
    <tr>
      <td><label for="txt5">Number 5</label>
      <input type="text" name="txt5" id="txt5" tabindex="5" value="0" onchange="calcAvg();"/></td>
    </tr>
    <tr>
      <td><label for="txtResult">averageResult</label>
      <input type="text" name="txtResult" id="txtResult" tabindex="6" readonly="readonly" /></td>
    </tr>
  </table>
</form>
</body>

Open in new window

0
 
dvcrduAuthor Commented:
many thanks to leakim971 for the fast replies.  i understood most of the coding.

many thanks to cyberkiwi -- i understood the entire solution.  

i didn't assign a local variable to each parameter within the calcavg function; didn't use the performcalc function to retrieve the info from the textboxes and put into a local variable that calls the calcavg function; didn't "print" the result to the txtresult txtbox.

thank you for helping me learn this!
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.