• 4
name Punditsdkoslkdosdkoskdo

Removing an thing from a select box


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>    
  • 0

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(); 
  • 0
Reply Report
window.onload = function ()
    var select = document.getElementById('selectBox');
    var delButton = document.getElementById('delete');

    function remove()
        value = select.selectedIndex;

    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;

addSelect.onclick = add;
  • 0
Reply Report


Related Questions

Trending Tags