javascript - jquery select div index -
let's assume have following divs
<div class="category"></div> <div class="item"></div> <div class="item"></div> <div class="category"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="category"></div> <div class="item"></div>
i access specific div class "category".
i know can use first:
$(".category").first()
but if want 2nd or 3rd? etc..?
you can use eq()
select element using index.
reduce set of matched elements 1 @ specified index.
$('.category').eq(2) // 3rd element
note: index starts zero.
demo
$('.category').eq(2).css('background', 'green');
.category { background: red; width: 100px; height: 100px; margin: 10px; float: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="category">a</div> <div class="item">b</div> <div class="item">c</div> <div class="category">d</div> <div class="item">e</div> <div class="item">f</div> <div class="item">g</div> <div class="item">h</div> <div class="category">i</div> <div class="item">j</div>
Comments
Post a Comment