Given a div such as:
<div class="widget block dock clock">yada yada</div>
I'd like to be abe to use JQUERY's $(div).attr('class');
to find all the classes, but the JQUERY.attr only return widget. Is there a way with JQUERY to return all the classes defined for the element?
Thanks
.attr('class') will return all classes defined for the element. Visual style is not only defined by the classes but also by css properties. You might also want to poll for them with .css('param-name')
Yup, it returns all
What you have currently will work, here's a quick demo:
Something to keep in mind is that
.attr()
returns that attribute on the first matched element, from the docs:So for example, something like this:
Would alert only
"widget"
, since that's the class of the first matching element.Nick mentioned using .each()...
...but...an easier way might be to iterate via .split():
...then you are just dealing with a plain old javascript string array