I'm trying to add a click event handler for each element that was created from JSON dynamically.
Here is my code :
$.getJSON('slides.json', function (data) {
for (var i = 0; i < data.length; i++) {
var s = sym.getSymbol("Base").createChildSymbol("Frame", "Slider");
s.$("Frame").css({
"background-image": "url('" + data[i].image + "')"
});
s.$("Frame").click("click", function (e) {
alert(data[i].title)
});
s.getSymbolElement().css({
"position": "absolute",
"right": i * 81 + 5 + "px"
});
sym.getSymbol("Base").$("Slider").css({
"width": i * 81 + 5 + "px"
});
} //for
});
This code would create 5 Frame with their image dynamically from JSON file. I want to be able to click on them and alert the index of the clicked image.
But in the code above, when I click on any image it always alerts 5, i.e. just the last value of i
.
I also tried to replace i
with data[i].title
but nothing happened, there is no access to the JSON file that can read information from that.
How can I solve this?