Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

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>

1 Answer

0 votes
by (40.7k points)

Try removing 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>

And, add an option like this:

$("#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>

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...