I have this code, where i create a object imagesAndName. this line will add data to the object
imagesAndName[0] = new Array("name", "image");
But in code below i receive data from an ajax call. Now i want to add this data to the object. But it doesn't work.
$(document).ready(function()
{
// create variables
var imagesAndName = {};
fillImagesAndNameObject();
function fillImagesAndNameObject()
{
$.ajax({
url : '../api/displayinformation.php',
contentType: 'application/json',
type: "POST",
dataType: "json",
data: 1,
success: function (data)
{
var present = data["teachers"];
$.each(present, function( key, value ) {
var name = value["name"];
var image = '<img src="'+ value["image"] +'"height="100" width="100"/>';
imagesAndName[key] = new Array(name,image);
});
}
});
}
});
I have create a callback but it still doesn't work
function fillImagesAndNameObject(imagesAndName, returnData) {
var json = JSON.stringify(
{
"method": "getAbsentTeachers",
"displayId": displayId
});
$.ajax({
url: '../api/displayinformation.php',
contentType: 'application/json',
type: "POST",
dataType: "json",
data: json,
success: returnData,
error: function () {
console.log('Something went wrong');
}
});
}
// create variables
var imagesAndName = {};
fillImagesAndNameObject(imagesAndName, function( returnValue ){
var present = returnValue["teachers"];
$.each(present, function( key, value ) {
var name = value["name"];
var image = '<img src="'+ value["image"] +'"height="100" width="100"/>';
alert(image);
imagesAndName[key] = new Array("test","test2");
});
});