Skip to content

Instantly share code, notes, and snippets.

@sillero
Last active January 4, 2016 20:08
Show Gist options
  • Save sillero/8671608 to your computer and use it in GitHub Desktop.
Save sillero/8671608 to your computer and use it in GitHub Desktop.
Enhancing Element.setAttribute() to accept only 1 argument for attribute creation
//test case http://jsfiddle.net/NHZ6W/
(function(Element, undefined){
var _setAttribute = Element.prototype.setAttribute;
Element.prototype.setAttribute = function(attrName, attrValue){
if (this.hasAttribute(attrName)) {
if (attrValue !== undefined) {
_setAttribute.call(this, attrName, attrValue);
}
//do nothing if attr exists and action is to create
}
else {
_setAttribute.call(this, attrName, (attrValue === undefined ? '' : attrValue));
}
}
})(Element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment