I'm looking to grab the current value of a div and on a toggle of another div add or subtract to that current value.
The flow would look something like this:
- Get current value
- On click once add '10'
- On second click subtract '10'
- Return new value
So far I've com up with this. The problem is that it goes from addition to subtraction through the first two clicks but on the third click it stays at subtraction.
$("#clickThis").click(function(e){
e.stopPropagation();
var costVal = parseInt($("#cost").text(),10);
var costNewVal = costVal + 22;
var costOldVal = costNewVal - 22;
$("#cost").toggle(
function(){
$(this).text(costNewVal);
console.log("Addition");
},
function(){
$(this).text(costOldVal);
console.log("Subtraction");
}
);
});