I have this problem with AJAX. I run the code and the ajaxString is undefined when the code is printed even though the Ajax code is pretty much called before the return statement. When I try to retrieve AJAX after the HTML generation and printing, the AJAX is defined so it makes me think that the return is called before AJAX gets the chance to finish. Any way around this? Here is my current code:
var ajaxHTML;
function runCode(str) {
if (str == 'SubmitB' || str == 'SubmitC' || str == 'SubmitD') {
generateHTML(str);
}
else {
$('#Holder1').html("");
$('#Holder2').html("");
$("#container_demo").fadeOut(500);
setTimeout(function(){$("#menu_container").html(generateHTML(str))},500);
$("#container_demo").fadeIn(500);
}
}
function generateHTML(source){
if (source =='d') {
return makeSchoolComboBox() + "Please Select a Level: <select id='selectedLevel'><option value='level1'>l1</option><option value='level2'>l2</option><option value='level3'>l3</option><option value='level4'>l4</option></select><br> <button id = 'r' onClick='runCode(this.id)'>Go back</button><button id = 'SubmitD' onClick='runCode(this.id)'>Submit</button>";
}
function makeSchoolComboBox() {
$.ajax({
type: 'GET',
url: 'phpQueries.php?q=fill_school_list',
success: function (data) {
ajaxHTML = data;
}
});
return ajaxHTML;
}