Basically I need a way to wait for the ajax call in the uniqueID()
function to finish before it returns a value to the while call in the createColumns()
function. Currently everything works fine however in HTML the id is undefined and about half a second after the functions are complete the id's that should be in the html are alerted.
The difference between my question and the one that was marked duplicate is that my call is in a while loop. Which means a callback function in success is not possible.
Please let me know if this doesn't make much sense. Thanks!
var uniqueid = "";
function uniqueID() {
uniqueid = "";
$.ajax({
url: '....',
type: 'post',
data: { "uniqueidfunc": "1"},
success: function(response) {
uniqueid = response;
alert(uniqueid);
return uniqueid;
}
});
}
var i = "";
var columns = "";
function createColumns(x) {
i = 0;
columns = "";
while (i < x) {
columns += '<div class="column col-'+x+'" id="'+uniqueID()+'"></div>';
i++;
};
return columns;
}
$(document).delegate('.tcolumn', 'click', function(){
colummnum = $(this).attr("id");
colummnum = colummnum.replace("tcolumn-", "");
$(this).parent().next().html(createColumns(colummnum));
});