Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Last active July 29, 2018 06:42
Show Gist options
  • Select an option

  • Save kitsune7/dd0f4ba536b5cc6778e6bfc742a3b01b to your computer and use it in GitHub Desktop.

Select an option

Save kitsune7/dd0f4ba536b5cc6778e6bfc742a3b01b to your computer and use it in GitHub Desktop.
Functions for getting siblings of a target node
function getSiblings (target) {
return [
...getUntilNull(target, 'previousElementSibling'),
...getUntilNull(target, 'nextElementSibling')
]
}
/**
* Collects HtmlElements until null is return from the provided property on target
*
* @params target: HtmlElement
* @params property: string
* @returns HtmlElement[]
*/
function getUntilNull (target, property) {
const collection = []
let next_target = target[property]
while (next_target !== null) {
collection.push(next_target)
next_target = next_target[property]
}
return collection
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment