473,657 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using javascript, calculate total, clear form and validate the form

17 New Member
Expand|Select|Wrap|Line Numbers
  1. <table style="width:50%">
  2. <tr>
  3.     <th>Product</th>
  4.     <th>Part Number</th> 
  5.     <th>Quantity</th>
  6.     <th>Unit Price</th>
  7.     <th>Subtotal</th> 
  8. <tr>
i want to calculate total of 5 items by clicking a button using javascript.
Sep 1 '19 #1
4 1254
gits
5,390 Recognized Expert Moderator Expert
what have you done so far to achieve this?
Sep 2 '19 #2
Zayn
17 New Member
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5.     var qtyBoxA = document.getElementById('quantityA');
  6.     var qtyBoxB = document.getElementById('quantityB');
  7.     var qtyBoxC = document.getElementById('quantityC');
  8.     var qtyBoxD = document.getElementById('quantityD');
  9.     var qtyBoxE = document.getElementById('quantityE');
  10.  
  11.     var subBoxA = document.getElementById('subtotalA');
  12.     var subBoxB = document.getElementById('subtotalB');
  13.     var subBoxC = document.getElementById('subtotalC');
  14.     var subBoxD = document.getElementById('subtotalD');
  15.     var subBoxE = document.getElementById('subtotalE');
  16.  
  17.     var CalculateTotal = document.getElementById('subtotalF');
  18.     var btnGetTot = document.querySelector("input[type=button]");
  19.     var btnReset = document.querySelector("input[type=reset]");    
  20.  
  21.  
  22.  
  23.     // Set up event handling in JavaScript, not HTML.
  24.     qtyBoxA.addEventListener("change", calc);
  25.     qtyBoxB.addEventListener("change", calc);
  26.     qtyBoxC.addEventListener("change", calc);
  27.     qtyBoxD.addEventListener("change", calc);
  28.     qtyBoxE.addEventListener("change", calc);
  29.     btnGetTotal.addEventListener("click", getCalculate);
  30.     btnReset.addEventListener("click", reset);/
  31.  
  32.     var gt = null; // Will hold the grand total
  33.  
  34.     function calc() {
  35.  
  36.       var priceA = 5;
  37.       var priceB = 10;
  38.       var priceC = 15;
  39.       var priceD = 20;
  40.       var priceE = 30;
  41.       gt = 0; 
  42.  
  43.       var qtyA = parseInt(qtyBoxA.value, 10);
  44.       var qtyB = parseInt(qtyBoxB.value, 10);
  45.       var qtyC = parseInt(qtyBoxC.value, 10);
  46.       var qtyD = parseInt(qtyBoxD.value, 10);
  47.       var qtyE = parseInt(qtyBoxE.value, 10);
  48.  
  49.       if (!isNaN(qtyA)) { subBoxA.textContent = qtyA * priceA; gt += +subBoxA.textContent; }
  50.       if (!isNaN(qtyB)) { subBoxB.textContent = qtyB * priceB; gt += +subBoxB.textContent; }
  51.       if (!isNaN(qtyC)) { subBoxC.textContent = qtyC * priceC; gt += +subBoxC.textContent; }
  52.       if (!isNaN(qtyD)) { subBoxD.textContent = qtyD * priceD; gt += +subBoxD.textContent; }
  53.       if (!isNaN(qtyE)) { subBoxE.textContent = qtyE * priceE; gt += +subBoxE.textContent; }
  54.  
  55.  
  56.       CalculateTotal.textContent = gt.toFixed(2);
  57.  
  58.     }
  59.  
  60.     function getCalculate(){
  61.       calc(); 
  62.       alert(gt);    
  63.     }
  64.       function reset(){
  65.  
  66.       subBoxA.textContent = "";
  67.       subBoxB.textContent = "";
  68.       subBoxC.textContent = ""; 
  69.       subBoxD.textContent = "";
  70.       subBoxE.textContent ="";
  71.       CalculateTotalTot.textContent = "";
  72.     }
  73.  
  74. </script>
  75. </head>
  76. <style>
  77. body {  
  78.   background-color:grey;
  79. }
  80. th{
  81.  
  82.   text-align: left;
  83. }
  84.  
  85. div{
  86.  
  87.   text-align: right;
  88.  
  89.   }
  90.  
  91.  .large{  
  92.          height:100px;
  93.          width: 170px;
  94.  
  95.  }
  96.   table{
  97.  
  98.   font-style: normal;
  99.   border-collapse: collapse;
  100.   }
  101. </style>
  102. <body>
  103. <form name="myForm" action="/submit.php" onsubmit="return validateForm()" method="post">
  104. <h1 style="text-align: left">Order Form</h1>
  105. <table>
  106.  
  107. <tr>
  108. <td>Name</td>
  109. <td><input type="text" name="fname" placeholder="Zayn" >
  110. </td>
  111.  
  112. <td> Surname</td>
  113. <td><input type="text" name="fsurname" placeholder="Saidi"></td>
  114. </tr>
  115.  
  116. <tr>
  117. <td>Email</td>
  118. <td><input type="text" name="fmail" placeholder="zaynsaidi@gmail.com"></td>
  119. <td>Cell Number</td>
  120. <td><input type="text" name="fcellnumber" placeholder="07712394400"></td>
  121. </tr>
  122. <tr>
  123. <td>Address</td>
  124. <td><input type="text" name="faddress" placeholder="" class="large"></td></div>
  125. </tr>
  126. <table>
  127. </tr><br><br>
  128. <form>
  129. <table style="width:50%">
  130. <tr class="wrapper">
  131.     <th>Product</th>
  132.     <th>Part Number</th> 
  133.     <th>Quantity</th>
  134.     <th>Unit Price</th>
  135.     <th>Subtotal</th>
  136. </tr>
  137. <tbody>    
  138. <tr>
  139.     <td>RAM</td>
  140.     <td>HP100</td> 
  141.     <td><input type="text" id="quantityA" maxlength="7" placeholder="60" ></td>
  142.     <td>ZW$5</td>
  143.     <td><input type="text" id="subtotalA" size="6" placeholder="$300" ></td>
  144.   </tr>
  145.  
  146.   <tr>
  147.     <td>Speaker</td>
  148.     <td>HP200</td> 
  149.     <td><input type="text" id="quantityB" maxlength="7" placeholder="65" ></td>
  150.     <td>ZW$10</td>
  151.     <td><input type="text" id="subtotalB" size="6"placeholder="$650" ></td>
  152.   </tr>
  153.  
  154.   <tr>
  155.     <td>Power Supply</td>
  156.     <td>HP300</td> 
  157.     <td><input type="text" id="QuantityC" placeholder="10" ></td>
  158.     <td>ZW$15</td>
  159.     <td><input type="text" id="subtotalC" size="6" placeholder="$150" ></td>
  160.   </tr>
  161.   <tr>
  162.     <td>HDD</td>
  163.     <td>HP400</td> 
  164.     <td><input type="text" id="quantityD" placeholder="20" ></td>
  165.     <td>ZW$20</td>
  166.     <td><input type="text" id="subtotalD" size="6" placeholder="$220" ></td>
  167.   </tr>
  168.   <tr>
  169.     <td>Motherboard</td>
  170.     <td>HP500</td> 
  171.     <td><input type="text" id="quantityE" placeholder="5" ></td>
  172.     <td>ZW$30</td>
  173.     <td><input type="text" id="subtotalE" size="6" placeholder="$150"></td>
  174.     </tr>
  175.     <tr>
  176.  
  177.     <th colspan="4"><div class="header"  >Total</div></th>
  178.     <td><input type="text" id="subtotalF" size="6" placeholder="$900"></td>
  179.     </tr>
  180.  
  181. </table>
  182. </form>
  183. <strong>Payment Details</strong><br>
  184. <strong>Methods</strong>
  185. <table>
  186. <tr>
  187. <td><input type="radio" name="gender" value="Visa" checked>Visa</td>
  188. <td>Card Number</td>
  189. <td><input type="text" name="fcardnumber" maxlength="30" placeholder="6079 9500 0013 7538">
  190. <td>Expiry Date</td>
  191. <td><input type="text" name="fexpirydate" placeholder="22/08/2030" maxlength="30">
  192. <tr/>
  193. <tr>
  194. <td><input type="radio" name="gender" value="Cash" checked>Cash</td>
  195. </tr>
  196. <tr>
  197. <td><input type="radio" name="gender" value="Ecocash" checked>Ecocash</td>
  198. </tr>
  199. <tr>
  200. <td><input type="radio" name="gender" value="Swipe" checked>Swipe</td>
  201. </tr><br><br><br>
  202. <table>
  203. <td>Delivery Address<br/><br/><br/></td>
  204. <td><input type="text" name="fdeliveryaddress" placeholder=""></td>
  205. </tr>
  206. </tr><br><br>
  207. <table>
  208. <tr>
  209. <td colspan="2" align="left" >
  210. <input type="button" name="claculate_form" value="Calculate" onclick="this.form.calculate();">
  211. <input type="reset" name="reset_form" value="Clear Form" onclick="this.form.reset();">
  212. </tr>
  213. <tbody>
  214. </table>
  215.  
  216.  
  217. </body>
  218. </html>
Sep 2 '19 #3
Zayn
17 New Member
its not executing.. help me guys
Sep 2 '19 #4
gits
5,390 Recognized Expert Moderator Expert
well - you have several problems in that code:
  1. you try to access the DOM before its fully loaded
  2. you have syntax error in Line 30
  3. in line 7 u try to access a node that cannot exist - id is case sensitive
  4. you try to call a function in line 210 - that function doesnt exist in the scope of the form from where you want to call it
  5. might be more issues - was only crossreading over the bunch of code ...

use the browser's developer tools and fix all the errors that show up in the console first.
Sep 2 '19 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1670
by: descds | last post by:
OK im prety much a newbie at PHP but it has me hooked. Im writing a comment system for one of our modules on phpnuke. Its all going very well, well should i say WAS going very well :) Ok all the comments work etc and everything is intergrated nicely in the database. Now im parsing the comments back out to the user and replacing string like :) with emoticons etc. It works perfectly and i'm seriously pleased with myself (ok ok remember i...
7
2815
by: NewbieJon | last post by:
I am attempting to send the variable "sComputerName" from my ActiveX script to "GetInfo.asp" using javascript. (Having been advised this is the way to get my ActiveX variable into my ASP script) My code is below. I am getting the following error "MyForm.oNetwork.Value is Null or is not an object". I'm struggling to diagnose what to do next. I'd very much appreciate some help Many thanks <HTML><BODY><FORM NAME="MyForm"...
2
2752
by: bbxrider | last post by:
i'm trying to pass a form object to a function to validate the forms user input when i try to access a forms properties-like .elements there is no response from the function and no error msg, i get a screen flash and if i try to preset an element property before calling the function like: this.firstName.optional=true; the code after that doesn't get executed going bananas, please help
10
19342
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes the server happy. I set up a copy of this form at my web site so that I could see exactly what a...
1
4477
by: mhawkins19 | last post by:
I have a form built and on the onclick event I validate all of the fields and then if the form is ok, on the submit event I run a javascript function to set a cookie and download a file from the current window. I have a cgi script provided by my web host to send the contents of the form through email but they only show me how to use the cgi script to send email through the submit event of the form. ie. <form name="downloadform"...
8
5724
by: Adam | last post by:
Hey, I'm using JS to submit a form with image submit buttons, using the following code... (Page is here... http://www.cards2do.co.uk/addcard.php?card_id=292 ) ************************************************************************************** <form action="https://www.cards2do.co.uk/addcard.php" method="post"
0
2171
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be edited and then saved. Currently, after saving the edited transaction and returning to the check register, the repeater control table returns to the bottom. I would like the edited transaction to appear in the table instead. I know that a...
3
2092
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to the original form to correct the input. This all works fine until I try to incorporate a javascript to display a popup calendar which posts the selected date back to a field on the form. This script works fine in itself, however if the page is...
1
1934
by: macintoshhondo | last post by:
Hi ! i am a newbie and dont know javascript much. what i really need is a simple javascript code that can insert number in the value section of the different forms from the one form. FORM 1: <form> <input type="text" name="textField" size="20"><br> </form> In the "TextField " of Form 1 i type any number and let the number be 100. Now what i really want is that in Form 2 : Textfield should be 100 that is just like that of form 1. In...
3
2228
by: scottmacd | last post by:
I'm sure this is a pretty simple question, so would be amazing if anyone could help me! I'm trying to make an HTML search form which can convert a human date into a UNIX timestamp date and include it in the search, alongside other inputs. Here's what I have currently: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html lang="en"> <head> <SCRIPT LANGUAGE="JavaScript">
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.