Skip to content

Instantly share code, notes, and snippets.

@zourtney
Created February 17, 2012 20:48
Show Gist options
  • Save zourtney/1855359 to your computer and use it in GitHub Desktop.
Save zourtney/1855359 to your computer and use it in GitHub Desktop.
Case insensitive $.removeAttr implementation. Based off jQuery 1.7.1.
(function($) {
var rcaps = /[A-Z]/,
rspace = /\s+/,
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
jQuery.fn.removeAttrI = function(value) {
return this.each(function() {
// Call normal `removeAttr`
if (! value.match(rcaps)) {
$(this).removeAttr(value);
}
// else: reimplement jQuery.removeAttr
var propName, attrNames, name, l,
i = 0,
elem = this;
if ( value && this.nodeType === 1 ) {
attrNames = value.split( rspace );
l = attrNames.length;
for ( ; i < l; i++ ) {
name = attrNames[ i ];
if ( name ) {
propName = jQuery.propFix[ name ] || name;
// See #9699 for explanation of this approach (setting first, then removal)
jQuery.attr( this, name, "" );
this.removeAttribute( jQuery.getSetAttribute ? name : propName );
// Set corresponding property to false for boolean attributes
if ( rboolean.test( name ) && propName in this ) {
this[ propName ] = false;
}
}
}
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment