Dispatching events is only useful when something listens to them. The element in question (#educationLevel) listens to focus, change, and blur events.
The easiest way to create events is with an Event constructor.
function dispatch(el, evType) {
el.dispatchEvent(new Event(evType));
}
var level = document.getElementById('educationLevel');
dispatch(level, 'focus');
level.selectedIndex = 1;
dispatch(level, 'change');
dispatch(level, 'blur');
However, #Program only listens to focus and blur so you don't need to dispatch the change event.
The actual problem: #Program is empty on load and only gets populated after change on #educationLevel fires. Thus you need to apply a mutation observer on it:
function initMO(root, callback) {
var MO = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MO(function(mutations) {
observer.disconnect();
mutations.forEach(function(mutation){
callback(mutation.target);
});
observe(); // comment this out to run only once
});
var opts = { childList: true, subtree: true };
var observe = function() {
observer.takeRecords();
observer.observe(root, opts);
};
observe();
}
full solution:
function dispatch(el, evType) {
el.dispatchEvent(new Event(evType));
}
function addMO(root, callback) {
var MO = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MO(function(mutations) {
observer.disconnect();
mutations.forEach(function(mutation){
callback(mutation.target);
});
observe(); // comment this out to run only once
});
var opts = { childList: true, subtree: true };
var observe = function() {
observer.takeRecords();
observer.observe(root, opts);
};
observe();
}
var level = document.getElementById('educationLevel');
dispatch(level, 'focus');
level.selectedIndex = 1;
dispatch(level, 'change');
dispatch(level, 'blur');
var program = document.getElementById('Program');
addMO(program, function() {
program.selectedIndex = 1;
});
<select>doesn't have any options other than the title until after the user selects the education level. Then all the options get added by Javascript. I suspect your extension is running before the options get added.changeevent listener for the education dropdown, after the page's ownchangelistener runs.