Skip to content

Instantly share code, notes, and snippets.

@kurisuwhyte
Created July 21, 2013 19:13
Show Gist options
  • Save kurisuwhyte/6049603 to your computer and use it in GitHub Desktop.
Save kurisuwhyte/6049603 to your computer and use it in GitHub Desktop.
Higher-order functions for computing the sibling of an element
function findElement(node, stepper) {
var x
return isRoot(node)? stepper(node)
: x = stepper(node)? x
: /* otherwise */ findElement(node.parentNode, stepper)
}
function findNextSibling(node) {
return findElement(node, function(el) { return el.nextSibling })
}
function findPrevSibling(node) {
return findElement(node, function(el) { return el.previousSibling })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment