Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active September 5, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/2f277e473dfc78866a143d8973b81dd9 to your computer and use it in GitHub Desktop.
Save nathansmith/2f277e473dfc78866a143d8973b81dd9 to your computer and use it in GitHub Desktop.
// Gets siblings of element.
const getSiblings = (el) => {
// Array method.
const f = Array.prototype.forEach
// Get children.
const children = el.parentNode.children
// Set in loop.
const siblings = []
// Loop through.
f.call(children, (item) => {
// Not element itself, nor text.
if (
item !== el &&
item.nodeType === 1
) {
// Add to array.
siblings.push(item)
}
})
// Expose array.
return siblings
}
// Export.
export default getSiblings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment