Skip to content Skip to sidebar Skip to footer

Calculating Xy-position Of Text Selection

I am trying to create my own text selection with DOM-elements. Yes, I mean the blue background you see behind the text when you select it in this element. The idea is to halt the d

Solution 1:

You can use getClientRnge:

var element = document.getElementById('element')

element.onmouseup = function(){
    var selection   = document.getSelection(),
        range       = selection.getRangeAt(0),
        clientRects = range.getClientRects()

    console.log(clientRects)
}

http://jsfiddle.net/XjHtG/

This will return the left, right, top, bottom, width and height of all selections made.

Post a Comment for "Calculating Xy-position Of Text Selection"