Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript select all siblings past an element

Tags:

javascript

I want to be able to select all siblings past a certain element, like this:

<ul>
    <li>Unselected</li>
    <li>Unselected</li>
    <li id="start">Start Here</li>
    <li>Selected</li>
    <li>Selected</li>
    <li>Selected</li>
</ul>

I cannot plan for the amount of siblings to select or the number at which it will start, so I'm guessing that there will need to be a .length in there at some point.

Vanilla JS only please :) Thanks!

like image 654
jasonetco Avatar asked May 31 '26 19:05

jasonetco


1 Answers

You can select those elements by using ~ selector,

var elems = document.querySelectorAll("#start ~ li");

DEMO

like image 172
Rajaprabhu Aravindasamy Avatar answered Jun 02 '26 08:06

Rajaprabhu Aravindasamy