Solved

Need to use multiplication with javascript

Posted on 2008-10-28
2
1,522 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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