Skip to content

Instantly share code, notes, and snippets.

@paulund
Created June 25, 2013 18:50
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save paulund/5861214 to your computer and use it in GitHub Desktop.
Save paulund/5861214 to your computer and use it in GitHub Desktop.
Add and Remove Options in Select using jQuery
$("#selectBox").append('<option value="option6">option6</option>');
$.each(selectValues, function(key, value) {
$('#mySelect')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
<select id="selectBox" name="selectBox">
<option value="option1"> option1 </option>
<option value="option2"> option2 </option>
<option value="option3"> option3 </option>
<option value="option4"> option4 </option>
</select>
$('select').children('option:not(:first)').remove();
$("#selectBox option[value='option1']").remove();
@victorvaz
Copy link

Perfeito! Parabéns!

@MickVazovsky
Copy link

Thanks

@isaiahiyede
Copy link

thanks really helpful

@chris-hall-hu
Copy link

very helpful, thanks muchly

@reshmashanbhag
Copy link

but if we do like this, after page reload the dropdown list value will be shown again

@geat
Copy link

geat commented Sep 22, 2016

thx

@rootturk
Copy link

rootturk commented Oct 3, 2016

thanks bro.

@ShiroTeky
Copy link

This works well.
$('#myselect').children('option').remove();
and after in your ajax request or using getJSON, bind the select like this:
$('#myselect').append(new Option(value.content, value.id, false, false));

@zamalhossain
Copy link

nice

@Magniveo
Copy link

Good, but how I can update select?

@wasimxe
Copy link

wasimxe commented Jan 4, 2018

I added new option successfully but when I lost focus and click select box again, newly added option gone. any idea why?

@reks-scripts
Copy link

reks-scripts commented Feb 28, 2018

Another way to write the example from add-options-from-array.js

$.each(selectValues, (key, value) => {
    $("<option/>", {
        "value": key,
        "text": value
    }).appendTo($("#mySelect");
});

@MatiasGallegos
Copy link

Remove all items:

$("#city_id").empty();

@baladkb
Copy link

baladkb commented Apr 11, 2018

thanks!.

@vivzon
Copy link

vivzon commented Oct 3, 2018

great, Thanks For Sharing.

@DarkGuardsman
Copy link

Thank you, this worked perfectly for what I needed

@mikbox74
Copy link

mikbox74 commented Oct 2, 2019

Thank you!

@CSEKU160212
Copy link

I want to remove one option and append another option in the same index.
Consider that:
<select id="selectBox" name="selectBox" >
<option value="option1"> option1 <option>
<option value="option2"> option2 <option>
<option value="option3"> option3 <option>
<option value="option4"> option4 <option>
<select>

After remove and append I want the options like:
<select id="selectBox" name="selectBox" >
<option value="myoption"> myoption <option>
<option value="option2"> option2 <option>
<option value="option3"> option3 <option>
<option value="option4"> option4 <option>
<select>

Is there any way to work with index?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment