jQuery - Get the Class of the child element clicked

Viewed 85

Let's say I have the following HTML:

<div id="wrapper">
  <div class="one">ONE</div>
  <div class="two">TWO</div>
  <div class="three">THREE</div>
</div>

If I have a click function as follows, how can I get the class name of the element that is clicked:

$(document).on('click', '#wrapper', function (event) {
    // get class name
});

So if I was to click TWO, it would return two

--

Just to add, the click handler would have to remain as #wrapper, so essentially I need to get the child element that was clicked...

1 Answers

Small Correction required. Trigger click event on child div.

$(document).on('click', '#wrapper div', function (event) {
   var classname =$(this).attr('class');
   alert(classname);

});
Related