4

I have an anchor as follows:

<a href="#" class="Menu">Text</a>

How can I get the CSS Class of this anchor using JQuery?

So in this case I would get "Menu".

I am able to add and remove CSS classes but I can't find a way to get the CSS classes of an element. What can I do ?

2
  • Duplicate stackoverflow.com/questions/1536117/… Commented Jul 25, 2013 at 15:14
  • I won't vote as duplicate as the linked answers mainly use the slow solution (that is attr) instead of the native property. Commented Jul 25, 2013 at 15:18

1 Answer 1

7

Use the native property :

$(selectorOrElement).get(0).className

Note that it's often answered to use attr('class'). Not only is there no gain in using a complex function instead of the native DOM property but it's also, quite naturally, much slower. See jsperf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.