javascript - Math Computation with Checked Boxes -
having trouble trying figure out how add value of checked box total. in case i'm trying add price of checked boxes price of original cost already. function totals cost of original cost correctly i'm not sure how add checked values function or create new one. in advance.
<tr> <th> small pizza </th> <td> $9.00 </td> <td> <input type = "text" name = "smal" id = "small" size ="2" /> </td> </tr> <tr> <th> medium pizza </th> <td> $11.00 </td> <td> <input type = "text" name = "medium" id = "medium" size = "2" /> </td> </tr> <tr> <th> large pizza </th> <td> $13.00 </td> <td> <input type = "text" name = "large" id = "large" size = "2" /></td> </tr> </table> <br><br> <!-- each topping $1 --> <input type ="checkbox" name = "pepp" id ="pepp" > pepperoni <br> <input type ="checkbox" name = "sausage" id ="sausage" > sausage <br> <input type ="checkbox" name = "pineapple" id ="pineapple" > pineapple <br> <input type ="checkbox" name = "bacon" id ="bacon" > bacon<br> <input type ="checkbox" name = "mushroom" id ="mushroom" > mushroom <br> <p> <input type = "button" value = "total cost" onclick ="computecost();" /> <input type = "text" size = "5" id = "txtcost" name = "txtcost" onfocus ="this.blur();" /> </p>
and javascript function
function computecost() { var small = document.getelementbyid("small").value; small = number(small); var medium = document.getelementbyid("medium").value; medium = number(medium); var large = document.getelementbyid("large").value; large = number(large); document.getelementbyid("txtcost").value = small*9 + medium*11 + large*13; }
this checked status of pepperoni boolean (true or false):
var pepp = document.getelementbyid("pepp").checked
in javascript, true
equivalent number 1, , false
equivalent number 0. assuming pepperoni costs additional dollar, cost this:
pepp * 1
… or more simply:
pepp
since toppings cost dollar per pizza, can calculate cost of pizzas (like you've done), add number of pizzas times cost of toppings:
var pepp = document.getelementbyid("pepp").checked, saus = document.getelementbyid("sausage").checked, pine = document.getelementbyid("pineapple").checked, bacon = document.getelementbyid("bacon").checked, mush = document.getelementbyid("mushroom").checked; document.getelementbyid("txtcost").value = (small*9 + medium*11 + large*13) + (small + medium + large) * (pepp + saus + pine + bacon + mush);
Comments
Post a Comment