What's the best way to find the first common parent of two DOM nodes in javascript?

Posted by sanchothefat on Stack Overflow See other posts from Stack Overflow or by sanchothefat
Published on 2010-03-16T10:59:30Z Indexed on 2010/03/16 11:16 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

My question is exactly that but in context I want to examine the selection object, compare the anchorNode and focusNode and if they are different then find the first common parent element.

var selected = window.getSelection();
var anchor = selection.anchorNode;
var focus = selection.focusNode;

if ( anchor != focus ) {
   // find common parent...
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom