Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active August 3, 2019 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/69a011d678625529a2aade6a8a1c823c to your computer and use it in GitHub Desktop.
Save matthewstokeley/69a011d678625529a2aade6a8a1c823c to your computer and use it in GitHub Desktop.
/**
* This method is several years old and is being produced here to continue the discussion
* about what is considered production-level element attribute toggling.
*
* Both jQuery and twbs are widely used in production sites and can be used
* as standards for production-level acceptance criteria.
*
* jQuery is similar to this method - it looks for
* classList first and then falls back to className.
*
* twbs4 only uses the classList object in components,
* possibly because classList has been a standard for several years.
*
*/
( function( _elements ) {
/**
*
* @param {Element} element
* @param {String} className
*/
var addClass = function( element, className ) {
if ( element.nodeType !== 1 ) {
throw new Error('Invalid Argument Type');
}
if ( element.classList && element.classList.add ) {
element.classList.add( className );
} else if ( element.className ) {
element.className = element.className + ' ' + className;
}
return element;
};
})( _elements );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment