I am not able to retrieve the data from my json object; the following code return undefined for var r in the console but the very similar code (shown below) works in JSFiddle.
var currentIMGArray = new Array();
$.ajax({
url:"foo.php",
type: "post",
data: {
action: "foo",
parameter: foo,
},
dataType: "json",
success: function (result) {
currentIMGArray = result[3];
console.log(currentIMGArray);
},
});
$(document).ready(function(){
$("#W").on("click", ".IMG", function() {
console.log("currentIMGArray: "+currentIMGArray.toString());
var IMGid = $(this).attr("id");
console.log("IMGid: "+IMGid);
var r = currentIMGArray.IMGid;
console.log("r: "+r);
})
})
Here is the console output:
[Log] {imgGallery0: "1.jpg", imgGallery1: "2.jpg"}
[Log] currentIMGArray: [object Object]
[Log] IMGid: imgGallery0
[Log] r: undefined
But this code works in JSfiddle so Im confused where the difference is ?
var arr = {imgGallery0: "1.jpg", imgGallery1: "2.jpg"};
var b = arr.imgGallery1;
console.log(b)