Re-populate drop down list options, how to clear options list and then re-populate?
When a event fires, I need to wipe out the current contents of the drop down list #users, and then re-populate it via ajax.
My ajax call is returning the HTML for the options like:
<option name=blah1>text1</option>
<option name=blah2>text2</option>
<option name=blah3>text3</option>
Use the :selected pseudo-selector to select the selected option from the dropdown and remove() to remove it from DOM.
We select all the options in the select box using the find() method with a parameter of “option“. Then, we remove all the selected options using the remove() method.
Select remove() Method The remove() method is used to remove an option from a drop-down list.
you can use the below
$('#mySelect').empty()
and then rebind the new data.
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