Skip to content

Instantly share code, notes, and snippets.

@tsouk
Created July 14, 2011 07:55
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 tsouk/1082075 to your computer and use it in GitHub Desktop.
Save tsouk/1082075 to your computer and use it in GitHub Desktop.
Get nearest anchor element
/**
@name getNearestAnchorElement
@private
@method
@param {Object} startNode The node to start the scan from
@param {Object} endNode The node to end on
@description Find the ancestor of startNode that is an anchor link (keeps searching up until the endNode)
*/
function getNearestAnchorElement(startNode, endNode){
var node = startNode;
do {
if (node.nodeName == 'A' || node.nodeName == 'INPUT') return node;
if (node === endNode) return false;
}
while(node = node.parentNode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment