I am having an issue with AJAX being the dominant action in a JQuery function. I would like this code to be somewhat linear and async doesn't seem to have an effect. What I am trying to do is display a "loader", and then hide said "loader" when the script has finished. I have many other instances of this but without the AJAX call. Instead I have used $.post, but for this specific instance $.ajax meets my needs better.
Desired result:
I would like to display the "loader" the instant "select_add_user".change is called. I would like the loader to stay put until the script (including the ajax) is finished.
Actual result:
The ajax loads FIRST without even displaying the loader, and then on "#select_sub_group".append the loader displays for a millisecond while the script appends my HTML.
Here is my script:
<script type="text/javascript"> $("#select_user_add").change(function(){ $("#loader:hidden").show("fast"); $('#select_sub_group').html(''); var appendD = ""; txt=$("#select_user_add").val(); $.ajax({ async: false, // For async, so it finishes running url: "get_ug.php", data: { id: txt }, type: "POST", dataType: "json", success: function(data){ $.each(data, function() { appendD = appendD + '<option id="usg' + this.id + '" value="' + this.id + '">' + this.label + '</option>'; $('#lgroup_' + this.id).css('background-color', '#CCFFCC'); }); } }); $('#select_sub_group').append(appendD); $("#loader").hide("fast"); }); </script> Racking my brain on this one .. It's either REALLY simple, or REALLY hard .. lol
async: falseand put$('#loader').hide("fast");inside yoursuccessfunction.