Last active
January 4, 2016 20:08
-
-
Save sillero/8671608 to your computer and use it in GitHub Desktop.
Enhancing Element.setAttribute() to accept only 1 argument for attribute creation
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
//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