javascript - Jquery: Number the occurance of equivalent class -


this short question.

if have multiple divs same class name:

<div class="apple">sweet </div> <div class="apple">healty </div> <div class="apple">nice </div> 

how can number class according occurance?

for example,

if hover on sweet, 1 should alerted. likewise, if hover on nice, 3 should alerted.

thanks in advance

using index():

$('.apple').hover(function(){     alert($(this).text() + " " + $(this).index()) }); 

working snippet

$('.apple').hover(function(){    alert($(this).text() + " " + $(this).index())  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="apple">sweet </div>  <div class="apple">healty </div>  <div class="apple">nice </div>


Comments