How to identify the child element clicked through the click event on the parent element

0

I have the following code:

<div class="tipo-cadastro">
    <div class="box-logista active">Logista</div>
    <div class="box-distribuidor">Distribuidor</div>
</div>

and the following code in javascript / jQuery:

$('.tipo-cadastro').click((e) => {
    $('.tipo-cadastro').find('div').removeClass('active');
    $(e.currentTarget).addClass('active');
});

My problem is that within my function e.currentTarget corresponds to the parent element, that is, the element that has the click event.

I know I could use the click event directly on each child element. But I would like to know if it is possible through the click event on the parent element to know which child element was clicked?

    
asked by anonymous 01.02.2017 / 18:31

1 answer

1

You can use target :

$('.tipo-cadastro').click((e) => {
  console.log(e.target);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="tipo-cadastro">
  <div class="box-logista active">Logista</div>
  <div class="box-distribuidor">Distribuidor</div>
</div>
    
01.02.2017 / 18:36