javascriptcoordinatesselectedtext

selected text and xy coordinates


how to get a selected text and xy coordinates of the word in the same time??


Solution

  • Just googled it:

    var txt = "";
    
    if (window.getSelection) {
        txt = window.getSelection();
    } else if (document.getSelection) {
        // FireFox 
        txt = document.getSelection();
    } else if (document.selection) {
        // IE 6/7 
        txt = document.selection.createRange().text;
    }
    
    txt = txt.toString()
    

    There is no simple way to get X/Y coordinates of the selected text. Because it dependes on its container position and size, text font, text layout, and many many other variables.