I have the following HTML
HTML
<h1 id="total"></h1>
<div id="option1" onclick="doMath()">Option 1</div>
<div id="option2" onclick="doMath()">Option 2</div>
And the following Javascript
JS
var basePrice = 0;
var optiononePrice = 5;
var optiontwoPrice = 10;
function doMath() {
$("#option1").click(function() {
basePrice += 5;
});
$("#option2").click(function() {
basePrice += 10;
});
};
$("#total").html(basePrice);
When a user clicks a div element, the price should add to the total, but this is what happens:
For example, the user clicks option1. The basePrice will print "5", but when they click option1 again, the basePrice goes from "5" to "15", then from "15" to "30" and goes like that every click. Same with option2, it starts at "10", then goes from "10" to "30" and so on.
What is happening, how should I fix this?
doMathoutside, deletedoMathand deleteonclick=doMath().