I did some reading about 'asynchronise ajax json calls' and I also watched some tutorials on youtube. After that I came up with the code below to return the response of the getJSON call in a global variable called fieldList
. But the objects in my var fieldList
stay empty. I think it is due to the asynchronise call being completed, after filling var fieldList
. But I'm lost on how to solve this at the moment. So can anyone help me with my code?
My .js file:
var fieldList = $("div.slctField");
$("#slctTable").change(function()
{
$.getJSON("dropdown_code/get_fields.php?table=" + $(this).val(), success = function(data)
{
var options = "";
for(var i = 0; i < data.length; i++)
{
options += "<option value='" + data[i] + "'>" + data[i] + "</option>";
}
$("#slctField").html("");
$("#slctField").append(options);
$("#slctField").change();
});
});
console.log(fieldList);
This is what I'm getting back at the moment(an empty object):
I checked what's in my select list now and first i tryed to get the options out of the list and that worked. I got the following output:
And when i check what was in the options i got the values like this:
But when i tryed to get all the values out of the dropdown with this code:
var values = $("select#slctAttribute option").map(function() {return $(this).val();}).get();
console.log(values);
I got this back in my console:
So it's still empty but I don't know why?