I have a list of elements, performing a function on each. Said function results in a subtotal which I wish to add to a grand total. Since I prefer to keep the window as unabused as possible, I declared the variable in my script right before the list declaration.
var total = 0;
$.each($("input[flag]:checked"), function(index, value){
var sub =+ value.attributes["blipp"].value;
total += sub; // this is window.total not my total!
});
I do remember that the scoping in JavaScript is less than well design, so I expected something unwanted to happen. And so I was right. The question is if it's possible to enclose the variable somehow and still keep it accessible form the individual function calls.