Reinstall the dropdown list, how to clear the list of options and then refill?

Re-populate the drop-down lists, how to clear the list of options and then refill?

When the event fires, I need to erase the current contents of the #users dropdown, and then refill it with ajax.

My ajax call returns HTML for the following parameters:

<option name=blah1>text1</option> <option name=blah2>text2</option> <option name=blah3>text3</option> 
+6
jquery ajax
source share
4 answers

You can simply change the html of the select element using the data returned from your ajax call

 $("#users").html(data); 

so that it looks something like this:

 $.ajax({ type: "POST", url: url, data: data, success: : function(data) { $("#users").html(data); } dataType: "HTML" }); 
+7
source share

you can use below

 $('#mySelect').empty() 

and then reformat the new data.

+24
source share
 function(ajaxResult) { $('#users').html(""); //clear old options $('#users').html(ajaxResult); } 
+2
source share

In the callback of your AJAX call, simply clear the contents of your select element and re-add the ones returned by the AJAX call.

 function(ajaxResult) { $('#users').html(ajaxResult); } 
0
source share

All Articles