javascriptjqueryjquery-selectorssiblings

Using jQuery can you select a set of siblings based on a number?


Example:

<span>Bacon, Chris P.</span>
<span>Jones, Pete</span>
<span>Adams, Sam</span>
<span>Bacon, Chris P.</span>
<span>Jones, Pete</span>
<span>Adams, Sam</span>
<span>Bacon, Chris P.</span>

Using eq() to choose where to start, how can one use jQuery to select an arbitrary number of siblings? Such as span:eq(0) through span:eq(10)?


Solution

  • jQuery slice():

    $('<span>').slice(1, 10);
    

    or you can use the :gt() selector combined with the :lt() selector:

    $('<span>:gt(0):lt(10)'); // first 10 elements
    

    Docs: