Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript help

Posted on 2010-09-07
8
Medium Priority
?
572 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
  • 4
  • 3
8 Comments
 
LVL 83

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 83

Expert Comment

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

on an input element ? to do what ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 83

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 83

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

916 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