Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Get the CSS class of an element

Tags:

jquery

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 ?

like image 387
Miguel Moura Avatar asked Mar 24 '23 01:03

Miguel Moura


1 Answers

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

like image 86
Denys Séguret Avatar answered Apr 07 '23 04:04

Denys Séguret