Created
September 20, 2015 14:23
-
-
Save jiahaog/22f2fd494705ad8992cf to your computer and use it in GitHub Desktop.
Change DOM dropdown index
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
/** | |
* Method that can be used to change dropdown index and also triggers an event to signal | |
* the change | |
* | |
* @param {string} selector | |
* @param {int} index | |
*/ | |
function changeDropdownIndex(selector, index) { | |
var element = document.querySelector(selector); | |
element.selectedIndex = index; | |
// event is needed because listeners to the change do not react to | |
// programmatic changes | |
var event = document.createEvent('Event'); | |
event.initEvent('change', true, false); | |
element.dispatchEvent(event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment