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...
}
I would try something like this, assuming no JS library:
using this utilities:
Then you can call
findFirstCommonAncestor(myElementA, myElementB).