For each loop change image by data element
I have a function that loops through all my img tags takes the data attribute and makes an AJAX call. The result of which determines what image each element should show.
It nearly works perfectly, but when it updates it replaces all the img with the same jpg.
$("button.checkStatus").click(function(){
//This Ajax checks the current on/off status of the passed X10 code
$('.checkStatus').each(function(i, obj) {
$x10Device = $(this).data("x10");
$.ajax({
url:"urlencode.php",
data: data,
type: "POST",
success: function(data) {
myd = $('<span />').html(data).find("#Result").text();
var Nmyd = myd.charAt(3);
if (Nmyd == '2'){$('img').attr('src','lightbulbon.png')}else{$('img').attr('src','lightbulboff.png')};
},
error: function (request, status, error)
{
alert(request.responseText);
}
});
});
});
I am thinking if Ichange the if statement to
if (Nmyd == '2'){$($x10Device).data('src','lightbulbon.png')}else{$('img').attr ('src','lightbulboff.png')};
It could work. I am not at my dev machine and it's niggling me.
thanks