Last active
December 17, 2015 13:54
-
-
Save koddsson/0c74f9ae7552d07cb23b to your computer and use it in GitHub Desktop.
Filter widget select boxes based on a criteria.
This file contains hidden or 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
// 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