440,782 Members | 1,154 Online
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

# Need help with simple price x quantity calculation

 P: 8 I have a very simple price times quantity calculation. The input is STWCORQuant: Expand|Select|Wrap|Line Numbers And the fucntion is STWCORPrice(): Expand|Select|Wrap|Line Numbers   function STWCORPrice() {         var price = 0;         var shipping = 0;         var total=0;         var qnty=document.all.STWCORQuant.value;           if (qnty > 49 && qnty < 75){                     price = 23;                     shipping = 6.25;                     }                   else if (qnty > 74 && qnty < 100){                     price = 17;                     shipping = 6.25;                         }                   else if (qnty > 99 && qnty < 200){                     price = 14;                     shipping = 6.25;                         }                   else if (qnty > 199 && qnty < 250){                     price = 13.75;                     shipping = 7.75;                         }           total = (price * qnty)+shipping;                 document.all.STWCORIssueCost.value= '\$ '+ total.toFixed(2);     }     The output is this: Expand|Select|Wrap|Line Numbers \$0 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 Replies

 Expert Mod 5K+ P: 8,639 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

 P: 8 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   function STWCORPrice() {         var price = 0;         var shipping = 0;         var total=0;         var qnty=GetElementById(STWCORQuant);           if (qnty > 49 && qnty < 75){                     price = 23;                     shipping = 6.25;                     }                   else if (qnty > 74 && qnty < 100){                     price = 17;                     shipping = 6.25;                         }                   else if (qnty > 99 && qnty < 200){                     price = 14;                     shipping = 6.25;                         }                   else if (qnty > 199 && qnty < 250){                     price = 13.75;                     shipping = 7.75;                         }           total = (price * qnty)+shipping;                 document.all.STWCORIssueCost.value= '\$ '+ total.toFixed(2);     }     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

 Expert Mod 5K+ P: 8,639 you don't have. in your case you have to call it as Expand|Select|Wrap|Line Numbers var qnty = document.getElementById("STWCORQuant"); to get the value of qnty add somewhere after its definition Expand|Select|Wrap|Line Numbers alert(qnty); this will show an alert box with qnty's value. regards Nov 20 '08 #4

 P: 8 OK...we have made some progress...I fixed the code per your suggestion. It is now: Expand|Select|Wrap|Line Numbers function STWCORPrice() {         var price = 0;         var shipping = 0;         var total=0;         var qnty=document.getElementById("STWCORQuant");         alert(qnty);           if (qnty > 49 && qnty < 75){                     price = 23;                     shipping = 6.25;                     }                   else if (qnty > 74 && qnty < 100){                     price = 17;                     shipping = 6.25;                         }                   else if (qnty > 99 && qnty < 200){                     price = 14;                     shipping = 6.25;                         }                   else if (qnty > 199 && qnty < 250){                     price = 13.75;                     shipping = 7.75;                         }           total = (price * qnty)+shipping;                 document.all.STWCORIssueCost.value= '\$ '+ total.toFixed(2);     }     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 var qnty=document.getElementById("STWCORQuant"); alert(qnty);   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   Any suggestions? Nov 20 '08 #5

 Expert Mod 5K+ P: 5,353 when you use: Expand|Select|Wrap|Line Numbers 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 var qnty = document.getElementById("STWCORQuant").value; or alert it as: Expand|Select|Wrap|Line Numbers alert(qnty.value) kind regards Nov 20 '08 #6

 P: 8 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 document.all.STWCORIssueCost.value= '\$ '+ total.toFixed(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 document.all("STWCORIssueCost").value= '\$ '+ total.toFixed(2);   The place where it goes on the page is: Expand|Select|Wrap|Line Numbers  \$0   Can you see why the total is not being displayed at STWCORIssueCost ? Nov 20 '08 #7

 Expert Mod 15k+ P: 16,027 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

 P: 8 It works...thank you very much! Nov 20 '08 #9

 Expert Mod 15k+ P: 16,027 Glad it does! Post again if you have more questions and we'll be happy to help. Nov 21 '08 #10

 Expert Mod 5K+ P: 8,639 @acoder I think, node.nodeValue should work also. regards Nov 21 '08 #11

 Expert Mod 15k+ P: 16,027 Actually, it wouldn't. You'd need to use firstChild.nodeValue in place of innerHTML. Nov 21 '08 #12

 Expert Mod 5K+ P: 8,639 you're right, I forgot the child status of the text node..... Dormi (doing currently too much PHP) Nov 21 '08 #13