Skip to content

Instantly share code, notes, and snippets.

@codenamejason
Created June 7, 2016 19:21
Show Gist options
  • Save codenamejason/45d709d8cb65576b8382e009e8309bb7 to your computer and use it in GitHub Desktop.
Save codenamejason/45d709d8cb65576b8382e009e8309bb7 to your computer and use it in GitHub Desktop.
// 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
@codenamejason
Copy link
Author

Add and remove selected options from an index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment