I need to find a way to select all content between two points in the view port. I need that selection to cross HTML elements as well.
Consider a page with two moveable boxes. These boxes are moved by the user around the content. They are absolutely positioned on the screen in front of all the content. When the boxes are moved, I need to select all content in between them. Has anybody come across anything similar? What I’m essentially trying to do is mimic smart phone selection handles.
I know there is a javascript function document.elementFromPoint(x, y). That won’t work in this case because the boxes will be selected as the top most elements at the point.
For WebKit and Opera, you could do this as follows:
document.caretPositionFromPoint(dragHandleX, dragHandleY)on each drag handleIn Firefox, mouse events have
rangeParentandrangeOffsetproperties that may help, though I think you’d need to somehow hide the the drag handles before the event fires.In IE, you could do something like this:
document.body.createTextRange()moveToPoint(dragHandleX, dragHandleY)on each TextRangesetEndPoint():firstTextRange.setEndPoint(secondTextRange)firstTextRange.select()