Created
June 25, 2013 18:50
-
-
Save paulund/5861214 to your computer and use it in GitHub Desktop.
Add and Remove Options in Select using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#selectBox").append('<option value="option6">option6</option>'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.each(selectValues, function(key, value) { | |
$('#mySelect') | |
.append($("<option></option>") | |
.attr("value",key) | |
.text(value)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('select').children('option:not(:first)').remove(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#selectBox option[value='option1']").remove(); |
Thank you!
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
Thank you, this worked perfectly for what I needed