Solved

javascript calculate multiple textboxes

Posted on 2009-05-09
7
723 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now