Index of the link with jquery

1

Let's suppose that throughout the sun I have several links with the same class, as I can know the index that is that link when clicking.

<a href="#seccion" class="section-modchooser-link"></a> // Este seria el indice 0
<a href="#ejemplo" class="section-modchooser-link"></a> // Este seria el indice 1
<a href="#otro-ejemplo" class="section-modchooser-link"></a> // Este seria el indice 2


$('a.section-modchooser-link').on('click',function(e){
    e.preventDefault()
    var clicked = $(this)
})

What I'm looking for: if I click on link 1, know that the link inside the sun is number 1

    
asked by César Alejandro M 27.04.2018 в 16:28
source

1 answer

4

The $.index() method returns the index of the item in jQuery.

$('a.section-modchooser-link').on('click', function(e) {
  e.preventDefault();
  var clicked = $(this).index();
  console.log(clicked)
})
<a href="#1" class="section-modchooser-link">1</a>
<a href="#2" class="section-modchooser-link">2</a>
<a href="#3" class="section-modchooser-link">3</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
answered by 27.04.2018 / 16:42
source