Get current element classes in jQuery
β<button id="test" class="example whatever">Show classes</button>βββββββββββββββββββββββββββββββββββββββββββββ
$('button').click(function(){
var classes = $(this).attr('class');
alert(classes);
});β
The code above returns the contents of the attribute class. How can I get it to return formatted classes?
For example, in this case, I want the variable to classeshave a value .example.whateverinstead example whatever.
I searched, and the only solution that seemed to be presented was the code I demonstrated above.
$('button').click(function(){
var classes = $(this).attr('class');
alert("." + classes.split(" ").join(".") );
});
Spell here: http://jsfiddle.net/wxDEh/3/