Sam Sam - 1 month ago 5
Javascript Question

Target elements that come after a specific element with CSS/JS

Using Javascript/jQuery or a CSS selector, how would I target elements that come after a specific element? To be clear, I don't mean directly after (adjacent to), I mean any and all matching elements that exist further down the page.

Imagine a group of

<p>
s:

<p>Lorem</p>
<p id="special">Ipsum</p>
<p>Dolor</p>
<p>Sit</p>
<p>Amet</p>


Now I want to target all of the
<p>
s that come after
p#special
... so the ones with content "Dolor", "Sit", and "Amet". I want there to be some selector available like:

$('p#special::allAfter p').each(...


How would I do this?

Answer

nextAll() is a jQuery function that you are looking for.

See this documentation:https://api.jquery.com/nextAll/