Solved

javascript calculate multiple textboxes

Posted on 2009-05-09
7
732 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

777 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