Skip to content

Instantly share code, notes, and snippets.

@koddsson
Last active December 17, 2015 13:54
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 koddsson/0c74f9ae7552d07cb23b to your computer and use it in GitHub Desktop.
Save koddsson/0c74f9ae7552d07cb23b to your computer and use it in GitHub Desktop.
Filter widget select boxes based on a criteria.
// Edit this object with the id of the select element and a array of values to
// keep.
var optionsToKeep = {
'activity-pcat256': [0, 1],
'activity-pcat258': [0, 2, 3],
'activity-pcat257': [0, 4, 5]
};
var filterSelectBoxes = function(options) {
var toDelete = [];
[].forEach.call(Object.keys(options), function(key) {
// Fetch the element from the DOM tree
var containers = document.querySelectorAll('.passenger-selector-container');
[].forEach.call(containers, function(container) {
var select = container.querySelector('#' + key);
// Add the elements to a array that don't fit the conditions
[].forEach.call(select.children, function(child) {
if (options[key].indexOf(Number(child.value)) === -1) {
toDelete.push(child);
}
});
});
});
// Delete all the elements in the array from the DOM tree
[].forEach.call(toDelete, function(option) {
option.remove();
});
};
// When the page is done loading all of the content
document.addEventListener('DOMContentLoaded', function() {
// Filter the select boxes according to the above options
filterSelectBoxes(optionsToKeep);
// create an observer instance
var observer = new MutationObserver(function(mutations) {
filterSelectBoxes(optionsToKeep);
});
// configuration of the observer:
var config = { subtree: true, childList: true };
// pass in the target node, as well as the observer options
observer.observe(document.querySelector('.passenger-selector-container'), config);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment