Skip to content

Instantly share code, notes, and snippets.

@Daniel-Wiedemann
Created January 23, 2014 17:01
Show Gist options
  • Save Daniel-Wiedemann/0c668fef95032029ab41 to your computer and use it in GitHub Desktop.
Save Daniel-Wiedemann/0c668fef95032029ab41 to your computer and use it in GitHub Desktop.
Get value and label from select HTMLElement
// <select id="test1">
// <option value="1">a</option>
// <option value="2">b</option>
// <option value="3">c</option>
// </select>
document.getElementById('test1').addEventListener('change', function(event){
var that = this; // or event.target
// if option has no value attribute, the event value returns the label
console.log('value: ' + that.value + ' | label: ' + that.selectedOptions[0].label);
// if first is selected it will return
// "value: 1 | label: a"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment