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
  • 659
  • 1
  • 6
  • 18
abu abu
  • 5,716
  • 12
  • 55
  • 108

2 Answers2

0

You can simply use jQuery index() function.

$(".addproduct").click(function(){
   console.log($(this).index());
});
Rado
  • 659
  • 1
  • 6
  • 18
0
$('addproduct').click(function(){
    $num = $(this).index();
    conosle.log($num+1);
});
MTakumi
  • 311
  • 2
  • 9