I am using a form in a modal where i add new fields to the select box and would like to refresh the select box such that the new options are added to it. And this without reloading the entire page . Can someone please tell me if this is possible or not and if it is how do i go about it.
Asked
Active
Viewed 2.8k times
0
-
is it simple select dropdown or some plugin you are using – Rahul Feb 21 '17 at 11:20
-
`$("form")[0].reset();`? – madalinivascu Feb 21 '17 at 11:20
-
Hi, Could you pls jsFiddle or something ? – Ranjeet Feb 21 '17 at 11:20
-
**Step 1**: Make API call and get new options. **Step 2**: Update selectbox with new options – Rajesh Feb 21 '17 at 11:20
-
Hey it is possible to reload select box however I would like to know what have you tried so far. If you have any code, please share. – Rupam Datta Feb 21 '17 at 11:20
-
http://stackoverflow.com/questions/18490026/refresh-reload-the-content-in-div-using-jquery-ajax – Aman Kumar Feb 21 '17 at 11:21
-
`$("#dropdownlist").empty();` – Afnan Ahmad Feb 21 '17 at 11:21
-
1Let us see at least the select and the new options. – Bulent Vural Feb 21 '17 at 11:22
2 Answers
5
There are multiple ways of doing this:
First if you are using jQuery this is simple like:
$("#dropdownlist").empty();
Another way can be:
for(i = dropdownlist.options.length - 1 ; i >= 0 ; i--)
{
dropdownlist.remove(i);
}
Another simplest way can be:
document.getElementById("dropdownlist").innerHTML = "";
Now if you want to repopulate it. You can append the options with jQuery. If you have single value you can achieve it like:
$('#dropdownlist').append($('<option>', {
value: 1,
text: 'New option'
}));
And if it's a collection. you need to loop over it like the following snippet:
$.each(newOptions, function (i, val) {
$('#dropdownlist').append($('<option>', {
value: val.value,
text : val.text
}));
});

Afnan Ahmad
- 2,492
- 4
- 24
- 44
2
Check this,
$(document).ready(function(){
$("#test").append($('<option>', {value: 2,text: 'Two'}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="test">
<option value="1">One</option>
</select>
Give it a try, it should work.

Rahul
- 18,271
- 7
- 41
- 60