274

How do I remove items from, or add items to, a select box? I'm running jQuery, should that make the task easier. Below is an example select box.

<select name="selectBox" id="selectBox">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option>    
</select>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Teifion
  • 108,121
  • 75
  • 161
  • 195

16 Answers16

500

Remove an option:

$("#selectBox option[value='option1']").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="selectBox" id="selectBox">
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option> 
</select>

Add an option:

$("#selectBox").append('<option value="option5">option5</option>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="selectBox" id="selectBox">
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option> 
</select>
TylerH
  • 20,799
  • 66
  • 75
  • 101
dsimard
  • 7,380
  • 4
  • 21
  • 25
  • 7
    If you need to remove an option and select another: $('#selectBox').val('option2').find('option[value="option1"]').remove(); – Radu Maris Feb 08 '12 at 09:35
  • 8
    Honestly, I think the way select boxes are manipulated via javascript is one of the most confusing things ever, even when you have a reasonably good understanding of selectors and the DOM. – Tacroy Apr 10 '12 at 00:07
82

You can delete the selected item with this:

$("#selectBox option:selected").remove();

This is useful if you have a list and not a dropdown.

rrk
  • 15,677
  • 4
  • 29
  • 45
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
  • It is removing everything for me... Wait I am using Select with multiple attribute. How I can remove it from multiple select ? – Akshay Nov 01 '18 at 13:08
28
window.onload = function ()
{   
    var select = document.getElementById('selectBox');
    var delButton = document.getElementById('delete');

    function remove()
    {
        value = select.selectedIndex;
        select.removeChild(select[value]);
    }

    delButton.onclick = remove;    
}

To add the item I would create second select box and:

var select2 = document.getElementById('selectBox2');
var addSelect = document.getElementById('addSelect');

function add()
{
    value1 = select2.selectedIndex;
    select.appendChild(select2[value1]);    
}

addSelect.onclick = add;

Not jQuery though.

John Odom
  • 1,189
  • 2
  • 20
  • 35
Adriana
  • 8,464
  • 13
  • 36
  • 37
25

To Remove an Item

$("select#mySelect option[value='option1']").remove(); 

To Add an item

$("#mySelect").append('<option value="option1">Option</option>');

To Check for an option

$('#yourSelect option[value=yourValue]').length > 0;

To remove a selected option

$('#mySelect :selected').remove(); 
Usman Shaukat
  • 1,315
  • 16
  • 22
20

This should do it:

$('#selectBox').empty();
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
qwe
  • 201
  • 2
  • 2
18

I find the jQuery select box manipulation plugin useful for this type of thing.

You can easily remove an item by index, value, or regex.

removeOption(index/value/regex/array[, selectedOnly])

Remove an option by
- index: $("#myselect2").removeOption(0);
- value: $("#myselect").removeOption("Value");
- regular expression: $("#myselect").removeOption(/^val/i);
- array $("#myselect").removeOption(["myselect_1", "myselect_2"]);

To remove all options, you can do $("#myselect").removeOption(/./);.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Zoredache
  • 37,543
  • 7
  • 45
  • 61
9

Adding/Removing value dynamically without hard-code:

// remove value from select dynamically

$("#id-select option[value='" + dynamicVal + "']").remove();

// Add dynamic value to select

$("#id-select").append('<option value="' + dynamicVal + '">' + dynamicVal + '</option>');
Muru Bakthavachalam
  • 1,340
  • 12
  • 8
8

I found two pages that seem helpful, it's written for ASP.Net, but the same stuff should apply:

  1. How to add/remove items from a dropdown list using jQuery
  2. jQuery selector expressions
Zachary Yates
  • 12,966
  • 7
  • 55
  • 87
5

Just to augment this for anyone looking, you are also able to just:

$("#selectBox option[value='option1']").hide();

and

$("#selectBox option[value='option1']").show();
AA11oAKas
  • 809
  • 1
  • 11
  • 23
5

The following links will be helpful-

http://api.jquery.com/remove/

http://api.jquery.com/append/

Anjuman
  • 1,424
  • 15
  • 10
4

JavaScript

function removeOptionsByValue(selectBox, value)
{
  for (var i = selectBox.length - 1; i >= 0; --i) {
    if (selectBox[i].value == value) {
      selectBox.remove(i);
    }
  }
}

function addOption(selectBox, text, value, selected)
{
  selectBox.add(new Option(text, value || '', false, selected || false));
}

var selectBox = document.getElementById('selectBox');

removeOptionsByValue(selectBox, 'option3');
addOption(selectBox, 'option5', 'option5', true);
<select name="selectBox" id="selectBox">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option> 
</select>

jQuery

jQuery(function($) {
  $.fn.extend({
    remove_options: function(value) {
      return this.each(function() {
        $('> option', this)
          .filter(function() {
            return this.value == value;
          })
          .remove();
      });
    },
    add_option: function(text, value, selected) {
      return this.each(function() {
        $(this).append(new Option(text, value || '', false, selected || false));
      });
    }
  });
});

jQuery(function($) {
  $('#selectBox')
    .remove_options('option3')
    .add_option('option5', 'option5', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="selectBox" id="selectBox">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
    <option value="option3">option3</option>
    <option value="option4">option4</option> 
</select>
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

I had to remove the first option from a select, with no ID, only a class, so I used this code successfully:

$('select.validation').find('option:first').remove();
nKn
  • 13,691
  • 9
  • 45
  • 62
1

   

$("#selectBox option[value='option1']").remove();
$("#selectBox").append('<option value="option1">Option</option>');
 when you want to totally remove and add option from select box then you can use this code

<select name="selectBox" id="selectBox">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>    
</select>

 $("#selectBox option[value='option1']").hide();
 $("#selectBox option[value='option1']").show();
sometime we need to hide and show option from select box , but not remove then you can use this code


<select name="selectBox" id="selectBox">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>    
</select>

$('#selectBox :selected').remove();
$("#selectBox option:selected").remove();
sometime need to remove selected option from select box then

<select name="selectBox" id="selectBox">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>    
</select>

('#selectBox').empty();
when you need to remove all option then this code

<select name="selectBox" id="selectBox">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>    
</select>

$('#selectBox').find('option:first').remove();
$('#selectBox').find('option:last').remove();
when need to first option or last then this code

<select name="selectBox" id="selectBox">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>    
</select>
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
0

I just want to suggest another way to add an option. Instead of setting the value and text as a string one can also do:

var option = $('<option/>')
                  .val('option5')
                  .text('option5');

$('#selectBox').append(option);

This is a less error-prone solution when adding options' values and texts dynamically.

polkduran
  • 2,533
  • 24
  • 34
  • I am not the downvoter but it appears that you answered OP halfway with how to add an option, but not how to remove an option. – John Odom May 07 '15 at 16:48
0

If somebody need delete ALL options inside a select, i did a litle function.

I hope you find it useful

var removeAllOptionsSelect = function(element_class_or_id){
    var element = $(element_class_or_id+" option");
    $.each(element,function(i,v){
        value = v.value;
        $(element_class_or_id+" option[value="+value+"]").remove(); 
    })
}

Only have to run

removeAllOptionsSelect("#contenedor_modelos");
daronwolff
  • 1,994
  • 21
  • 18
0

Perhaps someone else will need one elementary way to clear the select:

document.getElementById(SELECT_ID).innerHTML = "";
Никитос
  • 327
  • 2
  • 7