Annie
Annie

Reputation: 672

Finding the exact selected text index in html document which works on safari and google chrome

how to find index of selected text that is not dependent on window size and resolution? i have implemented it on IE but that code doesn't work on google chrome and safari as they dont support createrange method of javascript. this the code for IE to find start and end position

                range = doc.selection.createRange();
                range.execCommand("BackColor", false, colour);
                var sourceSelEnd = range.boundingLeft;
                var sourceSelStart = parseInt(sourceSelEnd);
                sourceSelStart = sourceSelStart - (range.text.length);

Please help in finding start index or position of selected text. Position() and Offset() are changing on window resize and resolution change

Upvotes: 1

Views: 1077

Answers (1)

Deets McGeets
Deets McGeets

Reputation: 6377

I believe the following should do what you are looking for though it is admittedly contrived to illustrate the functionality:

function getRangeAttributes()
{
    var currentRange = window.getSelection().getRangeAt(0);
    return {'startContainer':currentRange.startContainer,'startOffset':currentRange.startOffset,'endContainer':currentRange.endContainer,'endOffset':currentRange.endOffset,'textLength':currentRange.toString().length}
}

Please see the following for more information:

https://developer.mozilla.org/en/DOM/range

Upvotes: 1

Related Questions