Reputation: 2965
My HTML is as below.
<div class="col-md-12 line">
<span class="d1 available " seatid="515" seatname="A1"></span>
<span class="d1 available " seatid="516" seatname="A2"></span>
<span class="d1 available " seatid="555" seatname="A3"></span>
<span class="d1 available " seatid="556" seatname="A4"></span>
<span></span>
<span class="d1 available " seatid="517" seatname="A5"></span>
<span class="d1 available " seatid="518" seatname="A6"></span>
</div>
When I click on the span I want to get the index of span comparing to all other spans having same class ,
Which means..
This is the code I am using to take now.
var selector = $(this);
var selectIndex = selector.index();
Upvotes: 1
Views: 911
Reputation: 14712
you should passe the current element to index() to get it's index from all the current available span
so you can get the index by using $("span.available").index($(this))
PS: avoid using attribute directly on you html tag , you have to use the data- for both seatid and seatname => (data-seatid , data-seatname ) , you can access those by using dataset or data() in jquery
See below snippet
$(function() {
$("span.available").on("click",function(){
console.log("index : " + $("span.available").index($(this)) + " seatid: "+$(this).data('seatid') );
})
})
span {
display:block;
}
.as-console-wrapper {
max-height:75px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12 line">
<span class="d1 available " data-seatid="515" data-seatname="A1">A1</span>
<span class="d1 available " data-seatid="516" data-seatname="A2">A2</span>
<span class="d1 available " data-seatid="555" data-seatname="A3">A3</span>
<span class="d1 available " data-seatid="556" data-seatname="A4">A4</span>
<span>empty</span>
<span class="d1 available " data-seatid="517" data-seatname="A5">A5</span>
<span class="d1 available " data-seatid="518" data-seatname="A6">A6</span>
</div>
Upvotes: 1
Reputation: 2965
var selectIndex = selector.parent().children('span.available').index(selector);
Upvotes: 0