Solved

Need to use multiplication with javascript

Posted on 2008-10-28
2
1,521 Views
Last Modified: 2010-04-21
I have a field which a user will fill in the "number of tickets" and I need another field to automatically show the total.  The ticket price is  always the same ($20.00).
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

<!--

function GetVal()

{

      

      var Numtickets = document.dessert.Number_Of_Tickets.value;

      if(typeof Numtickets == "number")

      {

            

            document.frm.Total_amount_charged.value = numTickets * 20;
 

      }

      else

      {

            alert("Please enter numbers")

            return false;            

      }

      

}

//-->

</SCRIPT>

<form action="https://www.mywebserver.com/cgi-bin/formhandler.pl" method="post" name="dessert" id="dessert" onsubmit="GetVal()" >

<table>

                <tr valign="top" class="bodytext">

                  <td width="34%" align="right" class="bodytext">NUMBER OF TICKETS: <BR />($20.00 each)</td>

                  <td width="66%" class="bodytext"><input type="text" name="Number_Of_Tickets" size="30" maxlength="120" VALUE="" onchange="GetVal()">                 </td>

                </tr>

                <tr valign="top" class="bodytext">

                  <td width="34%" align="right" class="bodytext">TOTAL AMOUNT OF CHARGE:</td>

                  <td width="66%" class="bodytext"><input type="text" name="Total_amount_charged" size="30" maxlength="120" VALUE="" DISABLED />                  </td>

                </tr>

</table>

</form>

Open in new window

0
Comment
Question by:CalDev
2 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22823186
Personally I would use a span instead of a disabled textbox, but obviously that is up to you.

The following snippet is how I would do this:
<html>

<head>

	<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"><!--

		function GetVal()

		{

			var outputEl = document.getElementById('totalCharge');

			var Numtickets = parseInt(document.dessert.Number_Of_Tickets.value);

			outputEl.innerHTML = Numtickets * 20;

		}

	//--></SCRIPT>

</head>

<body>

	<form action="https://www.mywebserver.com/cgi-bin/formhandler.pl" method="post" name="dessert" id="dessert" onsubmit="GetVal()" >

		<table>

			<tr valign="top" class="bodytext">

				<td width="34%" align="right" class="bodytext">NUMBER OF TICKETS: <BR />($20.00 each)</td>

				<td width="66%" class="bodytext">

					<input type="text" name="Number_Of_Tickets" size="30" maxlength="120" value="" onkeyup="GetVal()">

				</td>

			</tr>

			<tr valign="top" class="bodytext">

				<td width="34%" align="right" class="bodytext">TOTAL AMOUNT OF CHARGE:</td>

				<td width="66%" class="bodytext">

					<span id="totalCharge"></span>

				</td>

			</tr>

		</table>

	</form>

</body>

</html>

Open in new window

0
 

Author Closing Comment

by:CalDev
ID: 31510785
That was spot on, great stuff, Thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
highcharts drilldown problem 6 335
asp.net upload progress bar 2 49
Problem to ToolkitScriptManager 2 30
jQuery aJax Disable Async 24 43
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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