My challenge is to select a range of DOM elements, starting from element
until element
. This can be done in jQuery like this: (Source)
$('#id').nextUntil('#id2').andSelf().add('#id2')
I want to achieve the same using JavaScript only.
Here's my attempt so far, but it seems to result in an infinite loop:
function prevRange(element, prevTill) {
var result = [];
while (element !== prevTill)
result.push(element);
return result;
}
var wrapper = document.getElementById('wrapper'),
wrapperChildren = wrapper.children;
console.log(prevRange(wrapperChildren[2], wrapperChildren[0]));
function prevRange(element, prevTill) {
var result = [];
/*while (element !== prevTill)
result.push(element);*/
return result;
}
<ul id="wrapper">
<li class="inner">I'm #01</li>
<li class="inner">I'm #02</li>
<li class="inner">I'm #03</li>
<li class="inner">I'm #04</li>
</ul>