473,487 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need help with calculating total based on combobox selections

1 New Member
I am currently creating a form that requires me to have multiple comboboxes which have various options with different values.

What I am having a problem with is finding a javascript snippet that will allow me to add the total value of all the comboboxes based on the selections.

I have an editbox at the bottom of the form where I would like the total value to appear.

I finally figured out how to calculate the form total based on the checkboxes and radio selections, but I can't find any javascript for comboboxes.

I would appreciate any type of help I can get.

Thanks!
Apr 14 '10 #1
3 2387
RamananKalirajan
608 Contributor
Hi Htidirect,
Its simple to get the value for the selection box. But it has the complex of how you are going to add.

sample html

Expand|Select|Wrap|Line Numbers
  1. <select id='mySelect' onchange='addValue(this)'>
  2.   <option value='1'>One</option>
  3.  <option value='2'>Two</option>
  4.  <option value='3'>Three</option>
  5.  <option value='4'>Four</option>
  6.  <option value='5'>Five</option>
  7. </select>
sample JS:
Expand|Select|Wrap|Line Numbers
  1.  
  2. function addValue(ths)
  3. {
  4.    alert(ths.options[ths.selectedIndex].value);
  5. }

This how you will be able to get the value from the select box. But onchange you have to minus the previous selection value and add the new value.

Thanks and Regards
Ramanan Kalirajan
Apr 14 '10 #2
mckatuni
1 New Member
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="Javascript">
  2.     var sum=0;
  3.     price = document.frmOne.select1.value;
  4.     document.frmOne.txtDisplay.value = price;
  5.     function OnChange(value){
  6.  
  7.         price = document.frmOne.select1.value;
  8.         quantity = document.frmOne.select2.value;
  9.         sum = price * quantity;
  10.  
  11.         document.frmOne.txtDisplay.value = sum;
  12.     }
  13. </script>
  14. <form NAME = "frmOne" action="initiateorder.php" method="post">
  15. Price:<br><INPUT name = "select1" TYPE = "Text" style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;" value ="2500" size = "35"><br>
  16. Quantity:<br><select name="select2" onchange='OnChange(this.value)' style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;">
  17.         <option value="1">1</option>
  18.         <option value="2">2</option>
  19.         <option value="3">3</option>
  20.     <option value="4">4</option>
  21.         <option value="5">5</option>
  22.         <option value="6">6</option>
  23.         <option value="7">7</option>
  24.         <option value="8">8</option>
  25.         <option value="9">9</option>
  26.         <option>10</option>
  27.     </select><br>
  28. Result:<br>
  29. <INPUT TYPE = "Text" name = "txtDisplay" size = "35" value ="" style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;" readonly><br><br>
  30. </form>
there you go "mckatuni"
Feb 20 '13 #3
jmrker
17 New Member
I read the initial request differently.
I thought OP wanted to sum MULTIPLE select boxes (comboboxes)
to arive at a total.

Here is my attempt:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>  </title>
  6. <!-- For: http://bytes.com/topic/javascript/answers/885630-need-help-calculating-total-based-combobox-selections -->
  7.  
  8. <style type="text/css">
  9.  #debugger { height:200px; width:300px; background-color:orange; border:1px solid blue; }
  10. </style>
  11.  
  12. </head>
  13. <body>
  14. <div id='debugger'>
  15.  <select id="SBox1" onchange="showOptions('colorPick',this)"></select> <input type="text" value="" id="colorPick"><p>
  16.  <select id="SBox2" onchange="showOptions('flavorPick',this)"></select> <input type="text" value="" id="flavorPick"><p>
  17.  <select id="SBox3" onchange="showOptions('fruitPick',this)"></select> <input type="text" value="" id="fruitPick"><p>
  18.  <select id="SBox4" onchange="showOptions('statePick',this)"></select> <input type="text" value="" id="statePick"><p>
  19. </div>
  20. <button onclick="calculate()">Summation</button> <input type="text" value="" id="summation" readonly>
  21.  
  22. <script type="text/javascript">
  23. Array.prototype.SBox = function(IDS) {
  24.   var str = '';  var tarr = [];
  25.   for (var i=0; i<this.length; ++i) {
  26.     if (this[i].indexOf('~') != -1) { tarr = this[i].split('~'); }
  27.                                else { tarr = [0,0]; tarr[0] = this[i]; tarr[1] = i; } // this[i]; } // default: selectedIndex | value
  28.     str += '<option value="'+tarr[1]+'">'+tarr[0]+'</option>';
  29.   } document.getElementById(IDS).innerHTML=str;
  30. }
  31. function calculate() {
  32.   var sum = 0;
  33.   var sel = document.getElementById('debugger').getElementsByTagName('select');
  34.   for (var i=0; i<sel.length; i++) {
  35.     sum += Number(sel[i].value);
  36.   }
  37.   document.getElementById('summation').value = sum;
  38. }
  39. function showOptions(ids,formInfo) {
  40.   var tmp = formInfo.value;
  41.   if (tmp != '~') { document.getElementById(ids).value = formInfo.value; }
  42.              else { document.getElementById(ids).value = ''; }
  43. }
  44. var Colors  = ['Choose color~','1 Red~1','2 Orange~2','3 Yellow~3','4 Green~4','5 Blue~5','6 Indigo~6','7 Violet~7'];
  45. var Flavors = ['Choose flavor~','1 Vanilla~1','2 Chocolate~2','3 Strawbery~3','4 Lime~4','5 Blueberry~5'];
  46. var Fruits  = ['Choose fruit','1 Apple~1','2 Bannana~2','3 Cantaloupe~3','4 Kiwi~4','5 Orange~5','6 Watermelon~6'];
  47. var States  = ['State~','1 Alabama~1','2 Alaska~2','3 Arkansas~3','7 Florida~7'];
  48.  
  49. window.onload = function () {
  50.   var str = '';
  51.   Colors.SBox('SBox1');
  52.   Flavors.SBox('SBox2');
  53.   Fruits.SBox('SBox3');
  54.   States.SBox('SBox4');
  55. }
  56. </script>
  57.  
  58. </body>
  59. </html>
  60.  
Mar 9 '13 #4

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

Similar topics

0
2151
by: Piet | last post by:
Hello wxPythoneers. I have a problem with a dialog box derived from wxFrame which has a wxComboBox as main element. Depending on the entry selected from the ComboBox, the dialog box will be...
5
3045
by: Steve | last post by:
I have an unbound combobox in the form header of a continuous form. The selection in the combobox sets the where clause in a querydef which determines QryPFrmInventoryManagement. The following code...
2
3135
by: Luther | last post by:
I want to create a form that searches a table. The hard part is this, I'd like to have the available records filtered based on combobox selections. For example, if this were a vehicle database, I...
6
4867
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
11
2777
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
2
1916
by: Greg Bradburn | last post by:
Greetings, I have a form with two comboboxes that I want to be populated with the same values from a table (i.e. both comboboxes have the same table/column as their datasource and display...
5
3470
by: Lars Netzel | last post by:
Hello! I have a number of files to copy. I have to total Amount of files and the Total FileSize of this operation (for example 3453 files, 4065.4 Mb)... How can I calculate how much time this...
4
4600
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
1
1549
by: pmarisole | last post by:
I need help in calculating a score from a row of drop-down values. I need to use the onChange to tally the score as the user moves across 9 categories (with drop-down selection of 1-9 or N/A)...
10
3768
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
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...
0
7181
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...
1
6846
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...
0
7349
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...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
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...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
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...

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.