473,503 Members | 1,655 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 2517
Dormilich
8,658 Recognized Expert Moderator Expert
you should use document.getElementById() 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(this.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.getElementById() 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(this.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 "STWCORQuant")

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.getElementById() 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
Dormilich
8,658 Recognized Expert Moderator Expert
@acoder
I think, node.nodeValue should work also.

regards
Nov 21 '08 #11
acoder
16,027 Recognized Expert Moderator MVP
Actually, it wouldn't. You'd need to use firstChild.nodeValue in place of innerHTML.
Nov 21 '08 #12
Dormilich
8,658 Recognized Expert Moderator Expert
you're right, I forgot the child status of the text node.....

Dormi
(doing currently too much PHP)
Nov 21 '08 #13

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

Similar topics

1
6069
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...
3
3152
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...
2
1699
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. ...
35
2247
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;...
2
1300
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...
1
1089
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,...
1
1656
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 ...
13
2060
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...
1
1986
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...
0
7086
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...
0
7280
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,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.