Jquery – How to get the class of the clicked element


I can't figure it out how to get the class value of the clicked element.

When I use the code bellow, I get "node-205" every time.


.find('> ul')
    selectedClass: 'active',
    select: function (event, ui) {
        //shows only the first element of list
    cookie: { expires: 0 },
    fx: fx


<ul class="tabs">
  <li class="node-205"></li>
  <li class="node-150"></li>
  <li class="node-160"></li>

Best Answer

Here's a quick jQuery example that adds a click event to each "li" tag, and then retrieves the class attribute for the clicked element. Hope it helps.

$("li").click(function() {
   var myClass = $(this).attr("class");

Equally, you don't have to wrap the object in jQuery:

$("li").click(function() {
   var myClass = this.className;

And in newer browsers you can get the full list of class names:

$("li").click(function() {
   var myClasses = this.classList;
   alert(myClasses.length + " " + myClasses[0]);

You can emulate classList in older browsers using myClass.split(/\s+/);