Solved

javascript calculate multiple textboxes

Posted on 2009-05-09
7
735 Views
Last Modified: 2012-05-06
Hi all,
I have a javascript that takes values of textboxes and calculates them out.  What I need to do is tweak my script so I can use multiple "products".  I was thinking of appending a unique string in the beginning of the name/id of each textbox then using match expression or preg (??? not sure if they are correct terms) to have the script find the unique sets and calculate them accordingly.
ie.
set1= QTY11_somename * PRICE11_somename
set2= QTY5999_somename * PRICE5999_somename
set3=QTYd_somename * PRICEd_somename
then have the script take the sum of all of the sets to calculate the totals...
In my script so far, the names are hard coded (only 1 set).  I need multiple sets with 1 set of totals... see my example:
<script type="text/javascript">
onload = function(){
document.getElementById("calc").onclick = function(){
var form = this.form;
form.subtotal.value = (form.qty.value * form.price.value).toFixed(2);
form.tax.value = (form.subtotal.value * form.taxrate.value).toFixed(2);
form.total.value = ((form.qty.value * form.price.value) + (form.subtotal.value * form.taxrate.value)).toFixed(2);
}
}
</script>
 
<form>
Quantity: <input name='qty' /><br>
Price: <input name='price' /><br>
Tax Rate: <input name="taxrate" value="0.0825"/><br>
Subotal: <input name="subtotal" readonly="readonly" /><br>
Tax: <input name="tax" readonly="readonly" /><br>
Total: <input name="total" readonly="readonly" /><br>
<input type="button" value="Calculate" id="calc" />
</form>

Open in new window

0
Comment
Question by:newbe101
[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
  • 5
7 Comments
 
LVL 6

Accepted Solution

by:
mudbuggle earned 250 total points
ID: 24345358
Are you using a server side language?
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 250 total points
ID: 24345507
I would assume newbe101 is using PHP since he mentions the preg functions (which is a PHP thing) and has asked many questions in the PHP zone.

In JavaScript the String.match function is used for that kind of thing: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/match

Keep in mind that the readonly attribute can be disabled, so you should always validate anything set in such a field, just like normal fields.
0
 

Author Comment

by:newbe101
ID: 24345545
yes... its php... and I have asked A LOT of questions in the php zone... lol.  I am going to checkout your link.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:newbe101
ID: 24345656
didn't get me anywhere... it just showed me how to seach a string, but I still don't know how to use it in my code...
0
 

Author Comment

by:newbe101
ID: 24348806
*bump*
0
 

Author Comment

by:newbe101
ID: 24375757
help, please...
0
 

Author Comment

by:newbe101
ID: 24384380
no more suggestions?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table header must be on top 2 57
What kind of script/language created this graph? 6 65
JSON help 8 46
Difference between Highcharts and Mapbox 10 44
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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