0

My HTML and jQuery codes are like below.

I would like to find out the number of instance of element clicked.

$(".addproduct").click(function() {

  //I would like to find out which element is clicked. First one ? or Second one ? or Third one ?

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="addproduct">product info</div>
<div class="addproduct">product info</div>
<div class="addproduct">product info</div>
<div class="addproduct">product info</div>
<div class="addproduct">product info</div>
Rado
  • 729
  • 1
  • 8
  • 20
abu abu
  • 6,599
  • 19
  • 74
  • 131

2 Answers2

0

You can simply use jQuery index() function.

$(".addproduct").click(function(){
   console.log($(this).index());
});
Rado
  • 729
  • 1
  • 8
  • 20
0
$('addproduct').click(function(){
    $num = $(this).index();
    conosle.log($num+1);
});
MTakumi
  • 320
  • 2
  • 10