I’m trying to read all class names from HTML element in the following way. But it doesn’t work:
HTML
<div id="test" class="fff aaa ccc" >hello world</div>
JS
if ($('#test').attr('class') != '')
{
// Read classes
var all_classes = $('#test').attr('class');
// Output
$('body').append('Classnames: '+all_classes+' ');
}
Example
http://www.jsfiddle.net/AQgqU/8/
Can somebody help me?
You can do this way:
Or you can use a loop:
Update:
As suggested by @KennyTM, with above code there was possibility of space coming around a class name. You can use the
$.trimfunction of jQuery to remove any space.