Last active
July 29, 2018 06:42
-
-
Save kitsune7/dd0f4ba536b5cc6778e6bfc742a3b01b to your computer and use it in GitHub Desktop.
Functions for getting siblings of a target node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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