Search code examples
jqueryfilterfindjquery-selectors

Find element but next of some element


How to find some element but next of some element ?

HTML :

<div id="container">
    <div class="box id_1">1</div>
    <div class="box id_2">2</div>
    <div class="break">{BREAK}</div>
    <div class="box id_1">1</div>     // <--- THIS !
    <div class="box id_2">2</div>     // <--- and should work when I detect in THIS also!
</div>

I using .next() and .filter() seem like it doesn't work :(

$('#container > .box').next('.break').filter('.id_1').addClass('found');

Playground : http://jsfiddle.net/l2aelba/2zBWv/


Solution

  • I think you can use .next() this way:

    $('#container > .break').next('.id_1').addClass('found');
    

    Find in fiddle here


    and if you want to affect .id_1 of only first .break then:

    $('#container > .break:first').next('.id_1').addClass('found');
    

    Find this in fiddle here


    Update:

    and if you want to affect .id_2 then .nextAll() should be used:

    $('#container > .break:first').nextAll('.id_2').addClass('found');
    

    Find .nextAll() in fiddle here