Solved

javascript help

Posted on 2010-09-07
8
515 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 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 100 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 400 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now