I'm running a database query to load a dropdownbox using jquery. Is there a way to display the words "Loading..." in the dropdownbox while the query is running?
Thanks.
You can add temporary item to your dropdown list while the ajax is running:
$('#myDropDown').prepend($('<option></option>').html('Loading...'));
Let's call your drop down 'userChoice', you can write code like
$(document).ready(
function()
{
//Before calling your ajax method, clear the select drop down.
//and add a loading option.
$('#userChoice')
.children()
.remove()
.end()
.append('<option value="">Loading...</option>');
$.ajax(
//Load the userChoice as usual in success handler of your ajax call.
success : function() { //Load #userChoice }
);
}
);
If there's nothing in there to begin with, just make that your default HTML and you've eliminated half of your jQuery.
<select>
<option>Loading...</option>
</select>
And when your query has finished, just replace the single option
with your results.
add this before ajax post
$('#myDropDown').empty();
$('#myDropDown').append($('<option></option>').html('Loading...'));
then again on success dynamically append dropdown
$('#myDropDown').empty();
$('#myDropDown').append($('<option></option>').val("").html("Select"));
for (var i = 0; i < array.length; i++) {
$('#myDropDown').append($('<option></option>').val(array[i].selectedvalue).html(array[i].selectedtext));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With