Created
June 7, 2016 19:21
-
-
Save codenamejason/45d709d8cb65576b8382e009e8309bb7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Get selected element | |
var s = document.getElementById('MySelect'); | |
// Index to add the option | |
var i = 0; | |
// New option('text', 'value') | |
s.options[i++] = new Option('text', 'value'); | |
s.options[i++] = new Option('Vermont', 'VT'); | |
// Set a selected index | |
s.selectedIndex = 1; | |
// Or | |
s.options[1].selected = true; | |
// Prints | |
console.log('State Abbr: ' + s.options[selectedIndex].value); | |
// To remove an option | |
s.options[0] = null; | |
// 2016 codenamejason |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add and remove selected options from an index.