?
Solved

Need to use multiplication with javascript

Posted on 2008-10-28
2
Medium Priority
?
1,530 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
[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
2 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 2000 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

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

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

764 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