Scone Scone - 1 year ago 66
Ajax Question

Associate JSON array element with event handler

Is there a way, as I iterate through a JSON file to associate certain array elements with click handlers for a list?

What I have is this:


url: 'json/data.json',
dataType : 'json',
type: 'get',
cache: false,
success: function(fullJSONData)
$(, oneJSONLine)
$('#eventList').append(newEvent(index, oneJSONLine))
$("#eventItem" + index).on("click", showEvent ([index]) );
error: function (e)
console.log("error " + e.message);


This is not working because all of the event handlers for showEvent() point to the last value that was in index. Can I get around this somehow?


Answer Source

You are invoking the function, not passing it as reference.


$(, oneJSONLine) {
    $('#eventList').append(newEvent(index, oneJSONLine))
    $("#eventItem" + index).on("click", function() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download