0

I write a function to calcluate the price of some books (that are selected in a list) but It doesn't work correctly.

function priceCal(bookid,price){    
   if (document.getElementById(bookid).checked) {
        document.getElementById('price').innerHTML=parseInt(document.getElementById("price").innerHTML)+price;
   }
}

I call it for examle in one book like this:

<input 
  type="checkbox" 
  name="bookList" 
  value="book3" 
  id="bookList_3"  
  onChange="priceCal('bookList_3','50');"
>

But it doesn't do anything! What's wrong in sending arguments?! The price is a <p> tag with 0 value (for default)

m59
  • 43,214
  • 14
  • 119
  • 136
  • Nothing's wrong with it. You should send `50` as a number instead of a string but other than that it's fine. [DEMO](http://jsfiddle.net/tewathia/C7zWy/) – tewathia Dec 14 '13 at 20:39

2 Answers2

0

Some browsers are only fireing the change event when the input loses focus. Use the click event instead. Also to make sure your code works as expected, always pass the radix param to the parseInt function (it's not 10 by default in all browsers).

You should also avoid passing price as a string. Just pass it as a number.

var someNum = parseInt('888', 10);
plalx
  • 42,889
  • 6
  • 74
  • 90
0

Change events only fire if you edit something in input and then blur it, or press enter. Put a console.log at the beginning of your function to check if it is getting to the function at all. Otherwise you can listen to a keydown or blur event, etc.

dthree
  • 19,847
  • 14
  • 77
  • 106