Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created April 17, 2013 19:05
Show Gist options
  • Save shaneriley/5406862 to your computer and use it in GitHub Desktop.
Save shaneriley/5406862 to your computer and use it in GitHub Desktop.
DOMTokenList Prototype Extensions
// Usage: `dom_element.classList.addAll(["foo", "bar"], "baz")` and `dom_element.classList.removeAll("foo", ["bar", "baz"])`
// See http://jsbin.com/obenin for demo
(function() {
var args;
var flattenArgs = function(collection) {
if (typeof collection === "string") {
args.push(collection);
return;
}
collection = Array.prototype.slice.call(collection);
collection.forEach(function(arg) {
if (typeof arg === "string") {
args.push(arg);
return;
}
if (typeof arg === "object" && arg.slice) {
flattenArgs(arg);
}
});
};
var changeAll = function(list, method) {
var t = this;
args = [];
flattenArgs(list);
args.forEach(function(val) {
t[method](val);
});
};
DOMTokenList.prototype.addAll = function() {
changeAll.call(this, arguments, "add");
};
DOMTokenList.prototype.removeAll = function() {
changeAll.call(this, arguments, "remove");
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment