Solved

javascript calculate multiple textboxes

Posted on 2009-05-09
7
734 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
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!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

735 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