Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript help

Posted on 2010-09-07
8
Medium Priority
?
562 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:dvcrdu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33623253
you're passing only one parameter
you should use onkeyup or "better" onblur (when you left the field) instead onchange
0
 

Author Comment

by:dvcrdu
ID: 33623306
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33623316
>i'm being required to use onchange

on an input element ? to do what ?
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:dvcrdu
ID: 33623370
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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 400 total points
ID: 33623414
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
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1600 total points
ID: 33623422
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
 

Author Closing Comment

by:dvcrdu
ID: 33623513
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33623523
You're welcome! Thanks for the points!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question