steve Kim steve Kim - 2 months ago 7
jQuery Question

jQuery eq() with different class name

I have following html:

<div class="block"></div>
<div class="block first"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block">this</div> // I need to target this to add another class
<div class="block"></div>


I want to select 3rd from the "block first" div:

$(".block.first").eq(3).addClass("last");


Of course I am missing something. The "first" class is dynamically added to one of the "block" div and I need to target 3rd div from where the "first" class is added. Anyone with suggestion? Thanks!

Answer

You could use $.fn.nextAll():

$('.block.first').nextAll('.block').eq(2).addClass('last');
Comments