/ Gists / prevAll, nextAll in pure jS (jQuery replacement)
On gists

prevAll, nextAll in pure jS (jQuery replacement)

JavaScript JS oneliners ES 6

index.html Raw #

  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li id="selected">6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
  </ul>

fns.js Raw #

let element = document.getElementById('selected')

const prevAll = (element) => { 
  let all = []
  while (element = element.previousElementSibling) {
    all.push(element)
  }
  
  return all
}

const nextAll = element => {
  let all = []
  while (element = element.nextElementSibling) {
    all.push(element)
  }
  
  return all
}


console.log(prevAll(element))
console.log(nextAll(element))
console.log(element)