1

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...

nsilva
  • 5,184
  • 16
  • 66
  • 108
  • See the answers to the [linked question](https://stackoverflow.com/questions/9012537/how-to-get-the-element-clicked-for-the-whole-document). Basically: `event.target.className` or if you want more jQuery, `$(event.target).attr("class")`. Or if you may have a `span` inside the `div` but you want to use `$(event.target).closest("div").attr("class")` (the DOM also has a `closest` method now). Etc. – T.J. Crowder Mar 05 '22 at 14:48

1 Answers1

0

Small Correction required. Trigger click event on child div.

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

});
Ajay Gupta
  • 703
  • 5
  • 11