user1119200 user1119200 - 2 years ago 166
jQuery Question

Select all elements between two specific elements

I need to select all

a
tags from
<a class="self">
to
<a class="next">
. Here is example

<span class="pages">
<a class="prev">&nbsp;</a>
<a>13</a>
<a>14</a>
<a>15</a>
<a>16</a>
<a class="self">17</a>
<a>18</a>
<a>19</a>
<a>20</a>
<a>21</a>
<a class="next">&nbsp;</a>
</span>


There are
href
s attributes. I don't want to get anything before class
self
. I want everything from
self
to end of the
span
element.

Answer Source

nextUntil [docs] would get all a elements between a.self and a.next, excluding them:

$('a.self').nextUntil('a.next', 'a');

If you want to include them, just use .nextAll [docs]:

$('a.self').nextAll('a').andSelf();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download