Link to home
Start Free TrialLog in
Avatar of dvcrdu
dvcrduFlag for United States of America

asked on

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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

you're passing only one parameter
you should use onkeyup or "better" onblur (when you left the field) instead onchange
Avatar of dvcrdu

ASKER

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 ?
>i'm being required to use onchange

on an input element ? to do what ?
Avatar of dvcrdu

ASKER

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
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dvcrdu

ASKER

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!
You're welcome! Thanks for the points!