473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can i get these elements to multiply then add and go into the total

flowerful
Expand|Select|Wrap|Line Numbers
  1. function process()
  2. {
  3. var book1=document.getElementById('amount_a').value*9;
  4. var book2=document.getElementById('amount_b').value*15.5;
  5. var book3=document.getElementById('amount_c').value*6;
  6. var book4=document.getElementById('amount_d').value*4;
  7. var book5=document.getElementById('amount_e').value*5.95;
  8. var book6=document.getElementById('amount_f').value*12.5;
  9. var book7=document.getElementById('amount_g').value*3;
  10. var book8=document.getElementById('amount_h').value*1.5;
  11.  
  12. total=book1+book2+book3+book4+book5+book6+book7+book8
  13. }
  14. {document.getElementById("tote").value=total;
  15. total="";
  16. }
Expand|Select|Wrap|Line Numbers
  1. //snippet of form
  2. <tr>
  3. <td id="book1">Instant Html</td>
  4. <td id="cost_a">9.00</td>
  5. <td width="40"><input class="txt"type="text" size="20"name="amount_a"id="amount_a"value="0"/></td>
  6. <td id="book2">Beginner's CSS Guide</td>
  7. <td id="cost_b">15.50</td>
  8. <td width="40"><input class="txt" type="text" size="20"name="amount_b"id="amount_b"value="0"/></td>
  9. </tr>
  10.  
Jun 3 '10 #1
6 1528
JKing
1,206 Expert 1GB
Hello,

Give this a try. I think it just might be what you are looking for.

Expand|Select|Wrap|Line Numbers
  1. function process()
  2. {
  3. var book1=document.getElementById('amount_a').value*9;
  4. var book2=document.getElementById('amount_b').value*15.5;
  5. var book3=document.getElementById('amount_c').value*6;
  6. var book4=document.getElementById('amount_d').value*4;
  7. var book5=document.getElementById('amount_e').value*5.95;
  8. var book6=document.getElementById('amount_f').value*12.5;
  9. var book7=document.getElementById('amount_g').value*3;
  10. var book8=document.getElementById('amount_h').value*1.5;
  11.  
  12. var total=book1+book2+book3+book4+book5+book6+book7+book8;
  13.  
  14. document.getElementById("tote").value=total;
  15. document.getElementById("tote").innerHTML = total;
  16. total="";
  17.  
  18. }
  19.  
Jun 4 '10 #2
@JKing
Hi JKing, thank you for your reply.I tried your idea and found it didn't work. I think I may have a problem with the button side of it. May I send you the whole thing?I am at a loss.As per usual it is probably something simple.
Jun 5 '10 #3
johny10151981
1,059 1GB
your all data are in <td>.
so try
Expand|Select|Wrap|Line Numbers
  1. document.getElementByID("****").innerHTML*your_value
if it dont work then
save all the data in hidden field as well as in <TD>
give new id to all those hidden field and then use
Expand|Select|Wrap|Line Numbers
  1. document.getElementByID("new_id").value*your_value
  2.  
It must have to work
Jun 5 '10 #4
acoder
16,027 Expert Mod 8TB
Use parseFloat() around the values to convert them from strings to floats. Also add spaces in your attributes:
Expand|Select|Wrap|Line Numbers
  1. size="20"name="amount_a"id="amount_a"value="0"
should be
Expand|Select|Wrap|Line Numbers
  1. size="20" name="amount_a" id="amount_a" value="0"
Jun 5 '10 #5
Hello again.So far I have got it so that it brings up the total, but only for a moment.So my problem being; the form is just the way I want it.except for total staying up and an alert box detailing the total and credit card used being displayed.
I would not know where to begin with hidden fields or parse Float.I have read about them but I am still new at this in a sense.
I would really appreciate any more help please.:)





[
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5.  
  6. <head>
  7.  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  9. <link href="tab.css" rel="stylesheet" type="text/css">
  10. <title>Tables</title>
  11.  
  12. <script type="text/javascript">
  13. function process_order()
  14. {
  15. var book1=document.getElementById('amount_a').value*9;
  16. var book2=document.getElementById('amount_b').value*15.5;
  17. var book3=document.getElementById('amount_c').value*6;
  18. var book4=document.getElementById('amount_d').value*4;
  19. var book5=document.getElementById('amount_e').value*5.95;
  20. var book6=document.getElementById('amount_f').value*12.5;
  21. var book7=document.getElementById('amount_g').value*3;
  22. var book8=document.getElementById('amount_h').value*1.5;
  23. var total=book1+book2+book3+book4+book5+book6+book7+book8;
  24. if(total==24)
  25. {
  26. alert("Please make a selection");
  27. return false;
  28. }
  29. total="$"+total;
  30. document.getElementById("tote").value=total;
  31. }
  32. </script>
  33.  
  34. <script> 
  35.  
  36. function validation(){
  37. valid=true;
  38. if (document.order.number.value=="")
  39. {
  40. alert ("Please enter your card number.");
  41. valid=false;
  42. }
  43. if(document.order.expires.value=="")
  44. {
  45. alert ("Please enter your expiry date.");
  46. valid=false;
  47. }
  48. return valid;
  49. }
  50. </script>
  51. <script>
  52. function thank_you()
  53. {                             
  54. valid=true;
  55. if (document.order.number.value="total")
  56. {
  57.   alert ("Thankyou" &nbsp "Your amount due is" &nbsp document.order.tote.value &nbsp 
  58.  
  59. "dollars."); 
  60. valid=false;
  61. }
  62. else if (document.order.choice.value="")
  63. {  alert("Your card is" &nbsp document.order.choice.value);
  64. valid=false; 
  65. }
  66. else
  67. {
  68.  alert("You haven't entered the fields.");
  69. valid=false;
  70. }
  71. return valid;
  72. }
  73. </script>
  74. <style type="text/css">
  75. div.c1 {text-align: center}
  76. </style>
  77. </head>
  78. <body>
  79.  
  80.  
  81. <form id="order" action="" name="order">
  82.  
  83. <table align="center" border="1" cellspacing="2" cellpadding="10" width="100%">
  84. <caption><h1>Favourite Books</h1></caption>
  85. <tr>
  86. <th align="center"colspan="6">ORDERS<br><br></th>
  87. </tr>
  88.  
  89.  
  90. <tr>
  91. <td width="250">Book</td>
  92. <td width="30"> Price </td>
  93. <td width="40">Quantity</td>
  94. <td width="200">Book</td>
  95. <td width="30">Price</td>
  96. <td width="40">Quantity</td>
  97. </tr>
  98.  
  99. <tr>
  100. <td id="book1">Instant Html</td>
  101. <td id='cost_a'>9.00</td>
  102. <td width="40"><input class="txt" type="text" size="20" name="amount_a" id="amount_a" value="0"></td>
  103. <td id="book2">Beginner's CSS Guide</td>
  104. <td id="cost_b">15.50</td>
  105. <td width="40"><input class="txt" type="text" size="20" name="amount_b" id="amount_b" value="0"></td>
  106. </tr>
  107.  
  108. <tr>
  109. <td id="book3">Xml Bible</td>
  110. <td id="cost_c">6.00</td>
  111. <td width="40"><input class="txt" type="text" size="20" name="amount_c" id="amount_c" value="3"></td>
  112. <td id="book4">Hot Dhtml Tips</td>
  113. <td id="cost_d">4.00</td>
  114. <td width="40"><input class="txt" type="text" size="20" name="amount_d"id="amount_d" value="0"></td>
  115. </tr>
  116.  
  117. <tr>
  118. <td id="book5">Cooking with Javascript</td>
  119. <td id="cost_e">5.95</td>
  120. <td width="40"><input class="txt" type="text" size="20" name="amount_e" id="amount_e" value="0"></td>
  121. <td id="book6">Flash into Flash</td>
  122. <td id="cost_f">12.50</td>
  123. <td width="40"><input class="txt" type="text" size="20" name="amount_f" id="amount_f" value="0"></td>
  124. </tr>
  125.  
  126. <tr>
  127. <td id="book7">Guide to buying on the<br>web </td>
  128. <td id="cost_g">3.00</td>
  129. <td width="40"><input class=txt type=text size="20" name="amount_g" id="amount_g" value="2"></td>
  130. <td id="book8">Web sites for<br>dummies</td>
  131. <td id="cost_h">1.50</td>
  132. <td width="40"><input class="txt" type="text" size="20" name="amount_h" id="amount_h" value="0"></td>
  133. </tr>
  134. </table>
  135.  
  136. <br>
  137.  
  138. <div class="c1"><input class="but1" type="submit" name="cal" id="cal" value="Calculate My Bill" onclick="process_order()"></center><br></div>
  139.  
  140. <table align="center" border="0" cellspacing="0" cellpadding="5" width="30%">
  141.  
  142. <tr>
  143.     <td>Total</td>
  144.    <td><input class="txt" type="text" name="tote" id="tote" value="24"></td>
  145. </tr>
  146.  
  147. <tr>
  148.     <td>Payment</td>
  149.   <td><select class="txt" id="choice" name="choice">
  150.  
  151.   <option value="Amex">Amex</option>
  152.   <option value="Diners">Diners</option>
  153.   <option value="Master Card">Master Card</option>
  154.   </select></td>
  155. </tr>
  156.  
  157.   <tr>
  158.     <td>Card Number</td>
  159.     <td><input class="txt" type="text" name="number" id="number" value="123456789" size="12"></td>
  160.   </tr>
  161.  
  162.   <tr>
  163.     <td>Expires</td>
  164.     <td><input class="txt" type="text" name="expires" id="expires" value="5/05" size="7"> 
  165.    </td>
  166.   </tr>
  167. </form>
  168. </table>
  169.  
  170. <br>
  171. <div class="c1"><input class="but2" type="submit" name="sub" id="sub" value="Submit"
  172.  onclick="validation() && thank_you();">
  173.  
  174. <input class="but3" type="reset" name="re" id="re" value="Clear" onclick="this.form.reset();">
  175. </div>
  176. </body>
  177. </html>
]
Jun 6 '10 #6
@flowerful
OK I have fixed my own problem. I changed the "calculate my bill" button to 'button' instead of 'submit'.
Fixed all my functions as well.I didn't need to parse float in this script.But intend to learn to do it for the next one.
Jun 13 '10 #7

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

Similar topics

5
by: Randell D. | last post by:
Folks, I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would appreciate some help.. I have an array - an example...
4
by: James Greig | last post by:
hello people, i'm just learning javascript, could someone point me in the direction of an example of the following, or give me some clues as to how it might be done: what i would like to do...
5
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question arose in a context that is not applicable to the...
1
by: Howard Jess | last post by:
Apparently, form elements of type <input type="image" src="...> are not included in the form's elements collection. I don't understand why not; according to DOM2, all form control elements...
11
by: Jon Hoowes | last post by:
Hi, I have inherited some code that has some form elements (radio buttons) that are called "1", "2" etc. for example: <input name="2" type="radio" value="45"> <input name="2" type="radio"...
3
by: Jody Greening | last post by:
How do I write a schema that is only validating elements: 1,4,7,8,9 and ignores every other element? I also need to not to care about the order of the elements or how many are in between...
0
by: manju | last post by:
Hi, I use DHTML browser as my main application window and I create elements like <p>, <table>, <img> etc dynamically using MSHTML C++ interfaces like IHTMLDocument2::createElement...
3
by: cweeks | last post by:
I am using the Webbrowser control in VS 2005 C# Express to automate the navigation of pages on a partner's web site. The first page contains an <IFRAME> element: <iframe style='width:40;...
2
by: libsfan01 | last post by:
hi all im looking for help on how you can track the order of elements in a parent div when these elements are dragable (with scriptaculous)? I tihnk i need to write a function that is called...
2
by: Simon Brooke | last post by:
I have a document type which I'm developing and working with, which is currently defined in a DTD, mainly because I still haven't really learned to use schemas. In this document type I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.