Created
July 16, 2010 05:28
-
-
Save boazsender/477970 to your computer and use it in GitHub Desktop.
classList() method which provides the behaviour you would expect from a method implementation of the HTML5 classList property which is supported currently in FF 3.6
This file contains 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
/* | |
Usages: | |
$(selector).classList() //returns an array of classnames | |
$(selector).classList('newclass') //replaces the current element's classes | |
$(selector).classList('new', 'class', 'names') //replaces the current element's classes | |
$(selector).classList(['new', 'class', 'names']) //replaces the current element's classes | |
*/ | |
jQuery.fn.extend({ | |
classList: function( value ) { | |
if ( jQuery.isArray( value ) ) { | |
// An array was passed, join it into a string. | |
value = value.join(' ') | |
} else if ( value ) { | |
// A non array was passed (either a sing string or multiple stings), | |
// join them into a string single string. | |
value = Array.prototype.join.call( arguments, ' ' ); | |
} else { | |
// No arguments were passed, return an array of class names. | |
return this.attr( 'class' ).split( /\s+/ ); | |
} | |
// Set class names and return the original jQuery object. | |
return this.attr( 'class', value ); | |
} | |
}); |
You’re right, I should’ve looked more closely. Your jQuery classList
does other stuff than HTML5 classList
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The thing is, that classList is a token list property of the html element and this is a method of the jQuery collection, so mapping the method to the native implementation might not make sense. But i'm going to think about it.