The Javascript/jQuery code below will output three divs with the numbers 1, 2 and 3. However when clicking any of the divs it will alert the number 3 always.
How would I write it so that it will alert the "correct" number. Without putting the number into the HTML itself.
var values = [1, 2, 3];
for (var i in values) {
var element = $("<div>" + values[i] + "</div>");
$("body").append(element);
element.click(function() { alert(values[i]); });
}