473,568 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with simple price x quantity calculation

8 New Member
I have a very simple price times quantity calculation.

The input is STWCORQuant:

Expand|Select|Wrap|Line Numbers
  1. <input name="STWCORQuant" class="contactitalicbold" id="STWCORQuant" onChange="javascript:STWCORPrice();" value="0" size="8" style="text-align: right; font-family: ver">
And the fucntion is STWCORPrice():

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.  
  3. function STWCORPrice() {
  4.         var price = 0;
  5.         var shipping = 0;
  6.         var total=0;
  7.         var qnty=document.all.STWCORQuant.value;
  8.  
  9.         if (qnty > 49 && qnty < 75){
  10.                     price = 23;
  11.                     shipping = 6.25;
  12.                     }
  13.  
  14.                 else if (qnty > 74 && qnty < 100){
  15.                     price = 17;
  16.                     shipping = 6.25;
  17.                         }
  18.  
  19.                 else if (qnty > 99 && qnty < 200){
  20.                     price = 14;
  21.                     shipping = 6.25;
  22.                         }
  23.  
  24.                 else if (qnty > 199 && qnty < 250){
  25.                     price = 13.75;
  26.                     shipping = 7.75;
  27.                         }
  28.  
  29.         total = (price * qnty)+shipping;        
  30.         document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
  31.     }
  32.  
  33. </script>
  34.  
The output is this:

Expand|Select|Wrap|Line Numbers
  1. <td width="15%" id="STWCORIssueCost" Font Class="sidemenutextgrn" align="right">$0</Font></td>
It doesn't work, but looks like it should. When a quantity is input and the box is tabbed out of, nothing happens. I can't tell what is wrong....need help!

Thanks
Nov 19 '08 #1
12 2529
Dormilich
8,658 Recognized Expert Moderator Expert
you should use document.getEle mentById() instead of document.all (that's IE syntax and not understood by many browsers).

rename onChange to onchange.

what is the value of qnty?

you could also call your function as STWCORPrice(thi s.value) (adjust function code...), so you don't need to fetch the value from the document tree.

regards
Nov 19 '08 #2
brossyg
8 New Member
you should use document.getEle mentById() instead of document.all (that's IE syntax and not understood by many browsers).

rename onChange to onchange.

what is the value of qnty?

you could also call your function as STWCORPrice(thi s.value) (adjust function code...), so you don't need to fetch the value from the document tree.

regards
I changed line five of the code per your suggestion:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function STWCORPrice() {
  3.         var price = 0;
  4.         var shipping = 0;
  5.         var total=0;
  6.         var qnty=GetElementById(STWCORQuant);
  7.  
  8.         if (qnty > 49 && qnty < 75){
  9.                     price = 23;
  10.                     shipping = 6.25;
  11.                     }
  12.  
  13.                 else if (qnty > 74 && qnty < 100){
  14.                     price = 17;
  15.                     shipping = 6.25;
  16.                         }
  17.  
  18.                 else if (qnty > 99 && qnty < 200){
  19.                     price = 14;
  20.                     shipping = 6.25;
  21.                         }
  22.  
  23.                 else if (qnty > 199 && qnty < 250){
  24.                     price = 13.75;
  25.                     shipping = 7.75;
  26.                         }
  27.  
  28.         total = (price * qnty)+shipping;        
  29.         document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
  30.     }
  31.  
  32.  
The value of qnty is the quantity input by the user (variable id is "STWCORQuan t")

It still doesn't work...and the onchange vs. OnChange made no difference when I tried it (all the other OnChange events are capitalized the same way).

Have I used the GetElementById correctly?
Nov 19 '08 #3
Dormilich
8,658 Recognized Expert Moderator Expert
you don't have. in your case you have to call it as
Expand|Select|Wrap|Line Numbers
  1. var qnty = document.getElementById("STWCORQuant");
to get the value of qnty add somewhere after its definition
Expand|Select|Wrap|Line Numbers
  1. alert(qnty);
this will show an alert box with qnty's value.

regards
Nov 20 '08 #4
brossyg
8 New Member
OK...we have made some progress...I fixed the code per your suggestion. It is now:
Expand|Select|Wrap|Line Numbers
  1. function STWCORPrice() {
  2.         var price = 0;
  3.         var shipping = 0;
  4.         var total=0;
  5.         var qnty=document.getElementById("STWCORQuant");
  6.         alert(qnty);
  7.  
  8.         if (qnty > 49 && qnty < 75){
  9.                     price = 23;
  10.                     shipping = 6.25;
  11.                     }
  12.  
  13.                 else if (qnty > 74 && qnty < 100){
  14.                     price = 17;
  15.                     shipping = 6.25;
  16.                         }
  17.  
  18.                 else if (qnty > 99 && qnty < 200){
  19.                     price = 14;
  20.                     shipping = 6.25;
  21.                         }
  22.  
  23.                 else if (qnty > 199 && qnty < 250){
  24.                     price = 13.75;
  25.                     shipping = 7.75;
  26.                         }
  27.  
  28.         total = (price * qnty)+shipping;        
  29.         document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
  30.     }
  31.  
  32.  
However, when the OnChange is triggered with an input, the result is an alert box that simply says: [Object]

So, the Onchange is working to call the function, but the input variable STWCORQuant is not making it to the alert correctly in these two lines:

Expand|Select|Wrap|Line Numbers
  1. var qnty=document.getElementById("STWCORQuant");
  2. alert(qnty);
  3.  
We are closer, but there seems to be a problem calling the variable and using it in the definition of qnty.

The input line is:

Expand|Select|Wrap|Line Numbers
  1. <input name="STWCORQuant" class="contactitalicbold" id="STWCORQuant" onChange="javascript:STWCORPrice();" value="0" size="8" style="text-align: right; font-family: ver">
  2.  
Any suggestions?
Nov 20 '08 #5
gits
5,390 Recognized Expert Moderator Expert
when you use:

Expand|Select|Wrap|Line Numbers
  1. var qnty = document.getElementById("STWCORQuant");
then qnty stores a reference to the node ... you just need to retrieve its value with:

Expand|Select|Wrap|Line Numbers
  1. var qnty = document.getElementById("STWCORQuant").value;
or alert it as:

Expand|Select|Wrap|Line Numbers
  1. alert(qnty.value)
kind regards
Nov 20 '08 #6
brossyg
8 New Member
Great! Now the alert correctly shows the value, but the output on the page remains $0 (nothing changes). The output code is:

Expand|Select|Wrap|Line Numbers
  1. document.all.STWCORIssueCost.value= '$ '+ total.toFixed(2);
  2.  
I know the code is now working up to this point because I "alerted" all the variables in the lines just before this and they all are correct.

I also tried:

Expand|Select|Wrap|Line Numbers
  1. document.all("STWCORIssueCost").value= '$ '+ total.toFixed(2);
  2.  
The place where it goes on the page is:

Expand|Select|Wrap|Line Numbers
  1.  <td width="15%" id="STWCORIssueCost" Font Class="sidemenutextgrn" align="right">$0</Font></td>
  2.  
Can you see why the total is not being displayed at STWCORIssueCost ?
Nov 20 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
Two issues:
1. Use document.getEle mentById() in place of document.all.
2. Use innerHTML in place of .value because a td element doesn't have a value.
Nov 20 '08 #8
brossyg
8 New Member
It works...thank you very much!
Nov 20 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
Glad it does! Post again if you have more questions and we'll be happy to help.
Nov 21 '08 #10

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

Similar topics

1
6073
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping Cart</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link rel="StyleSheet" href="css/style.css" type="text/css">
3
3158
by: ChadDiesel | last post by:
Hello everyone. I need some advice on table structure for a new project I've been given. One of our customers sends us an Excel spreadsheet each week containing their order. Currently, someone formats the spreadsheet, prints it out, and manually picks out the products we need to ship. I want to import this into an Access table. ...
2
1702
by: Shannan Casteel via AccessMonster.com | last post by:
I have a subform for listing parts. It has fields including: ClaimID, ITEM, NET PRICE, LIST PRICE, Quantity, Supplier, and a calculated field called Part Total. The subform is based on a query. The Part Total field is calculated as (=.*.). Then there is a hidden text box on my subform that calculates the sum of all the Part Totals for...
35
2263
by: eyoung | last post by:
I call a function that takes the unit price and quantity ordered to create an amount...it looks something like this. function calculateCost() { quantity=document.RFO.quantity.value; unitPrice=document.RFO.unitPrice.value; total=0; if(isPositiveInteger(quantity)) {
2
1306
by: Srikanth | last post by:
Hi, My requirement is to have the datagrid perform the work of an order entry system. The specifications are something like this. I have got a table in MS-Access, named master_software, whose fields are: soft_name, edition, manufacturer, price. My data grid would contain the following columns: Software Name,
1
1092
by: lucky29105 | last post by:
Hello- I cant seem to figure out why I am getting the following error: cannot find symbol : class CD location: class Inventory5 CD cd = (CD)supplies; ^ I'm getting this on several lines, and something tells me it is a silly error, but I can't find it. Here is the part of the code where I get the errors: CD cd = (CD)supplies;
1
1658
by: wynn | last post by:
I had created a shopping cart website using visual studio 2005 web developer I got this error when i doing update the shopping cart, can anyone please help me solve this problem thanks very much Dim cn As New SqlClient.SqlConnection() cn.ConnectionString =...
13
2062
by: jcato77 | last post by:
I am having trouble figuring out my code and was hoping someone could point me in the right direction. Below is my code what I need to due is create a method to add and display the value of the entire inventory. I have what I think is the correct code but it's not working. Help is greatly appreciated class Inventory3 { public static...
1
1995
by: Luron31 | last post by:
Hello everyone, im doing a project and im really stuck on it... i dont want answers. i have a very guilty conscience. i just need some hints as to what im doing wrong or what i could do right... "A VendingMachine object represents a vending machine that sells products like candy or soda. It has a fixed number of slots, each of which can...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.