0

Site:

http://tinyurl.com/ah9hkgz

Issue: I'm trying to write simple javascript function to calculate subtotals and totals of order for customer. I got arrows to increment/decrement values for each product.

Tried to write function cash(qty); that is called onclick together with other "inline javascript" codes. On site, only first product's UP arrow is calling cash function.

When function cash is called, the document.getelementbyid('szett_subtotal').Value=price; doesn't work.

cash function:

function cash(qty)
{
   var price = 0;
   price = 12990 + ((qty.value-1)*9990); 
   document.getelementbyid('szett_subtotal').Value=price;
)

Thanks for your help. I hope.

Clarification

Down button does not contain this cash function call because I am focusing on up button for test purpose.

1
  • I fixed the case-sensitivity as Lion suggested. The down button is not issue. I was testing it on up button first. When that is sorted, I believe the down button will be easy stuff to complete ;-) Commented Feb 5, 2013 at 23:32

3 Answers 3

1

Your function declaration of cash causes a parse error:

function cash(qty)
{
   var price = 0;
   price = 12990 + ((qty.value-1)*9990); 
   document.getElementById('szett_subtotal').value=price; 
)
^
Uncaught SyntaxError: Unexpected token )

Function body should end with a curly end brace }.

Also, don't use inline JavaScript code, it's error prone and adding behaviour from outside the HTML is preferred, such as .addEventListener().

The easiest to do is create a click handler function and call that from inline:

function inc_quantity(v)
{
  var qty_el = document.getElementById(v);
  var qty = qty_el.value;
  if( !isNaN( qty )) qty_el.value++;
  cash(qty_el);
}

  ... onclick="inc_quantity('quantity_szett')"
Sign up to request clarification or add additional context in comments.

3 Comments

I opened Firebug and wondered what cash not defined means.. you saved hours of my time waste !!! THANKS
@Skuta Take note of the second part of my answer as well; it should help reduce those big swathes of inline code you have now :)
Yep, I'll think about it - if it's needed as there are only few sites like this i have :D
0

Value should be value, and getelementbyid should be getElementById.

document.getElementById('szett_subtotal').value=price; 

JavaScript is a case-sensitive language.

7 Comments

Concentrate on the function identifier - getElementById()
According to the asker, it works for the up button but not the down button. We need to find a difference in the code for the up and down buttons. For example, note the absence of a method call on the down button's onclick in the linked source code. Good point, though, about the method calls--I did not notice that.
Math, sorry, it is not working for up button (and I do not care for down button yet as up doesn't work yet).
@Skuta Change getelementbyid to getElementById. Captalize the E, B, and I.
Yes, I changed the case-sensitivity on the website and copy pasted your code to be megasure. Doesn't work. :(
|
0

On the down button, your page has:

var qty_el = document.getElementById('quantity_szett'); var qty = qty_el.value; if( !isNaN( qty ) && qty > 0 ) qty_el.value--;return false;

On the up button, however, it says:

var qty_el = document.getElementById('quantity_szett'); var qty = qty_el.value; if( !isNaN( qty )) qty_el.value++;cash(document.getElementById('quantity_szett'));return false;

Call the cash function on the down button's onclick. Computers have no common sense and will not call cash for the down button even if the up button does.

I.E. add

cash(document.getElementById('quantity_szett'));

right before the return false of your down button's onclick.

Hope this helped!!

6 Comments

Does this reply mean you can see the value change if you press up button? Cause I can't.
I can't replace it with qty.value because qty.value is number of products and "szett_subtotal" is where I want the price to go.
I did not test the page--I just looked at the source and saw that you didn't call cash. Follow Lion's suggestion too--change Value to value and getelementbyid to getElementById. Also, did you intend to not call cash? The question implied that the only problem was that the down button didn't call cash, and that that was unwanted--sorry if I misinterpreted.
I understand your confusion with my post, the thing is -> I need to fix why UP button doesn't work. Don't worry to click on the page, there is no virus there and it is the first product, first up arrow :)
I did those changes right now on website (case sensitivity) and nothing changed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.