I have the following JavaScript function:
$('div[id^="event"]').each(function( index ) {
fullID = $(this).attr('id');
object = $(this);
idOnly = fullID.replace( /^\D+/g, '');
var jqxhr = $.ajax({
type: 'POST',
url: '/welcome',
data: {
'lat': lat,
'lon': lon ,
'eventID': idOnly
},
success: function(msg){
object.show();
}
});
});
What I want this piece of code to do is the following :
For each div id which has an ID which starts with event, get the number portion of the div element id (for example one could have an id of 'event39410') - so get the '39410' piece - send that along with two variables to an AJAX service - and when the response comes back, show the div with id 'event39410'.
The ajax component works fine - the problem is however, that if i have 10 of these event tags, I get 10 ajax calls to my backend, but I only ever get the last 'event' div shown - which suggests that by the time the success function gets called the object is always referencing the last entry in the loop.
Can somebody help me to fix this variable scoping issue?