How to remove and replace select options using jquery?
You can do the following:
var selectbox = $('#city');
selectbox.empty();
var list = '';
for (var j = 0; j < i.length; j++){
list += "<option value='" +i[j].name+ "'>" +i[j].name+ "</option>";
}
selectbox.html(list);
Note: Don't call the append method in the loop and also cache the selectors.
this is the native java-script correct working form:
// get the select html element by id
var selectElement = document.getElementById('city');
// remove all options from select
selectElement.options.length = 0;
// create new option element
var newOptionElement = document.createElement('option');
newOptionElement.value = "optionValue";
newOptionElement.innerHTML = "option display text";
// add the new option into the select
selectElement.appendChild(newOptionElement);
Working Fiddle example
Removes all options and appends your default one again:
var select = $('#city');
select.empty().append('<option value="">---Select City---</option>');
http://api.jquery.com/empty/