take this simple code:
<div id="container">
<div class="box">abc</div>
<div class="box" id="secondbox">abc</div>
<div>generic</div>
<div>generic</div>
</div>
Now I add the class box to let’s say the last div generic:
$('#container div:last').addClass('box');
Now if i try to select the next .box with this it doesnt’ work:
$('#secondbox').next('.box')
returns .length=0
I presume what you actually mean is
#container div:last.nextdoes not find the next element that matches a selector. It finds the next sibling element. If you supply a selector, it tests the element against that selector. If the test fails, an empty selection (i.e.length == 0) is returned.You need
nextAll: