html - JavaScript function Not Working with onchange attribute of input tag -
i have been trying replicate duncan donut example head first javascript
, function subtotal()
never triggered onchange event , when html reference, did not find onchange event in list provided.
duncan.html
<html> <head><title>duncan online donut's service</title></head> <script type="text/javascript"> function subtotal(){ document.write("working"); const taxrate = 0.095; const donutrate = 0.5; var tax = 0; var subtotal = 0; var total = 0; var cakedonut = parseint(document.getelementbyid("cakedonut").value); var glazedonut = parseint(document.getelementbyid("glazedonut").value); if(isnan(cakedonut)) cakedonut = 0; if(isnan(glazedonut)) glazedounut = 0; subtotal = (cakedonut + glazedonut)* donutrate ; tax = subtotal * taxrate ; total = subtotal + tax ; document.getelementbyid("subtotal").value = "$" + subtotal.tofixed(2); document.getelementbyid("tax").value = "$" + tax.tofixed(2); document.getelementbyid("total").value = "$" + total.tofixed(2); } </script> <body> <h1><b><i>duncan online donut's service</i></b></h1> <form> name : <input id="name" type="text" name="name"/><br><br> #no of cake donuts : <input id="cakedonut" type="text" name="cakedonut" onchange="subtotal()"/><br><br> #no of glazed donuts : <input id="glazedonut" type="text" name="glazedonut" onchange="subtotal("/><br><br> subtotal : <input id="subtotal" type="text" name="subtotal" /><br><br> tax : <input id="tax" type="text" name="tax" /><br><br> total : <input id="total" type="text" name="total"/><br><br> <input type="submit"/><br><br> </form> </body> </html>
jsfiddle http://jsfiddle.net/o6vhzfcy/1/
the above code. tried running on ie , chrome
in vain. in advance.
the problem have defined variable , function both same name subtotal , variable declaration overriding function definition. change function name subtotal1 work.
Comments
Post a Comment