Skip to content

Instantly share code, notes, and snippets.

@jiahaog
Created September 20, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiahaog/22f2fd494705ad8992cf to your computer and use it in GitHub Desktop.
Save jiahaog/22f2fd494705ad8992cf to your computer and use it in GitHub Desktop.
Change DOM dropdown index
/**
* 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