Skip to content Skip to sidebar Skip to footer

Get Elements In Text Selection

I want to get all of the elements contained in a user selection (as in DOM 2 ranges / MS TextRanges). /** @return {Array.} */ function getSelectedElements() { var

Solution 1:

After getting some sleep and reading up on compareBoundaryPoints again, I think I have the answer.

if (elementRange.compareBoundaryPoints(Range.START_TO_START, selectedRange) > -1 &&
    elementRange.compareBoundaryPoints(Range.END_TO_END, selectedRange) < 1) {

This seems to only evaluate to true for elements that fall completely within the user selection.


Post a Comment for "Get Elements In Text Selection"