$(document).ready( function() {
var box = $('.box');
box.click( function() {
console.log($(this).index());
});
});
div, section {
border: 1px solid red;
width: 80px;
height: 80px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">1</div>
<div class="box">2</div>
<div class="para">3</div>
<div class="box">4</div>
In the above HTML, the last div with class .box is the 3rd .box and I want the index of the last .box to be 3, but I get 4. Which jQuery function should I used instead which will search all divs with class box and then return me the position of .box in the set on clicking the .box?