What is the most efficient and elegant way to select all elements between two specified elements using vanilla JavaScript? For instance, how can I target all elements between:
<h2>
and <hr>
.. and the result should be an [ p, p, ul ]
... but
<p>this should not be found</p>
should not be included since there is no after it.
While I could potentially write a script to check siblings and iterate through them, I am hoping for a more elegant solution that I may not be aware of. It would be ideal if there was some sort of querySelectorAll(..) selector manipulation that I could utilize, perhaps through an intersection of two invocations.
<div>
<h1>Title</h1>
<h2>Subitle</h2>
<p>Para 1 <a href="#">link</a></p>
<p>Para 2</p>
<ul><li>A list</li></ul>
<hr/>
<h2>Another subtitle</h2>
<p>this should not be found</p>
</div>