What you have currently will work, here's a quick demo:
alert($("div").attr("class")); //alerts "widget block dock clock"
Something to keep in mind is that .attr()
returns that attribute on the first matched element, from the docs:
It's important to note that the .attr()
method gets the attribute value for only the first element in the matched set. To get the value for each element individually, we need to rely on a looping construct such as jQuery's .each()
method.
So for example, something like this:
<div class="widget">yada yada</div>
<div class="widget block dock clock">yada yada</div>
Would alert only "widget"
, since that's the class of the first matching element.