Skip to content

Instantly share code, notes, and snippets.

@bkardell
Last active March 21, 2016 19:49
Show Gist options
  • Save bkardell/ad0fba550663b9ebbdea to your computer and use it in GitHub Desktop.
Save bkardell/ad0fba550663b9ebbdea to your computer and use it in GitHub Desktop.
Rewind like 5 years no one was thinking much about a11y, developers I mean but developers were doing a lot of js on the dom dom has a simple API - hasAttribute, getAttribute, setAttribute and it has this one very special attribute 'class' which is a space delimited series so that was a big pain in the ass if you want to say 'does it have this cl…
HTMLElement.prototype._tokenListFor = function(attr) {
var el = this,
getTokens = function() {
return (el.hasAttribute(attr)) ? el.getAttribute(attr).split(' ') : [];
},
tokList = {
contains: function(token) {
var contains = getTokens().some(function(item) {
return item === token;
});
return contains;
},
add: function(token) {
var refs = getTokens();
if (!this.contains(token)) {
refs.push(token);
}
el.setAttribute(attr, refs.join(' '));
},
remove: function(token) {
var refs = getTokens().filter(function(item) {
return item !== token;
});
el.setAttribute(attr, refs.join(' '));
}
};
return tokList;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment