jquery - Javascript: How to Dynamically Update My Closure? -
i new js so, apologies if dumb question:
objective: each time user selects car drop down menu price of car increments running total.
i have jsfiddle of code here: update closure
<select id="select"> <option value="">select</option> <option value="100.00">volvo</option> <option value="200.00">saab</option> <option value="300.00">mercedes</option> <option value="400.00">audi</option> </select> <p>total:</p> <p id="total"> </p> javascript:
var addto = function(passed) { var add = function(inner) { return parseint(passed) + parseint(inner); } return add }; var inner = 0; // need dynamically able update value increments each new car selected var addprod = new addto(inner); $("#select").change(function() { var car = $("#select").val(); $("#total").append(addprod(car)); }); so, grab selected product. closure adds product price 0 start.
i select car. second car price must added first, incrementing total. total displayed on webpage.
sounds should easy. code in fiddle not work. not know how code increment "inner" value of closure. inner must persist between each selection.
i think you're looking for.
var runningtotal = 0; $("#select").on('change', function() { var car = parseint($(this).val()); runningtotal += car; $("#total").html(runningtotal); });
Comments
Post a Comment