3

I tried the jquery-fieldselection plugin to get the selected text in textarea.

It works fine in Firefox and Chrome, but not in Internet Explorer 7.

I use the getSelection() method like this:

textarea.getSelection();

When the text within the textarea is 12345, and all this text is selected, Firefox and Chrome returns:

start: 0    // Correct!
end: 5

while Internet Explorer 7 returns:

start: 5    // Why ??
end: 5

I'm looking for a cross browser solution using jQuery.

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

2 Answers2

6

just took a look a tthelibrary and it behaves differently for IE since it does not support some methods that the modern browsers do.. may be the code there isnt perfect..

use the following method:

function getInputSelection(el) {
    var start = 0, end = 0, normalizedValue, range,
        textInputRange, len, endRange;

    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
        start = el.selectionStart;
        end = el.selectionEnd;
    } else {
        range = document.selection.createRange();

        if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                start = end = len;
            } else {
                start = -textInputRange.moveStart("character", -len);
                start += normalizedValue.slice(0, start).split("\n").length - 1;

                if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                    end = len;
                } else {
                    end = -textInputRange.moveEnd("character", -len);
                    end += normalizedValue.slice(0, end).split("\n").length - 1;
                }
            }
        }
    }

    return {
        start: start,
        end: end
    };
}

how to use it:

you need the dom object of the textarea.. thus:

var textArea=    $("textarea")[0] or getElementbyId("textareaid");

var selectedText=getInputSelection(textArea);

var start=selectedText.start;
var end=selectedText.end;

Live Demo

ram1
  • 6,290
  • 8
  • 41
  • 46
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • btw this method was used by us in one our projects and I am not sure of its origin – Baz1nga Aug 25 '11 at 09:54
  • I tried to change your `` with a `
    `, and it doesn't work in IE7. Could you say why? Demo here: http://jsfiddle.net/RbSLw/1/
    – Misha Moroshko Aug 25 '11 at 12:01
  • This function is by me. I've posted it in various places on Stack Overflow. Here's one example: http://stackoverflow.com/questions/263743/how-to-get-cursor-position-in-textarea/3373056#3373056 – Tim Down Aug 25 '11 at 13:09
  • Adding `unselectable="on"` to the `
    ` solved the problem.
    – Misha Moroshko Aug 25 '11 at 14:08
4

Here is a very concise and compact jquery plugin that works for IE7,IE8,IE9,FF,Chrome.. Allows you to get/set start & end position of a selection and also replace selected text programmatically. A working example/code version is here: http://jsfiddle.net/hYuzk/3/ A version with more detailed comments etc. is here: http://jsfiddle.net/hYuzk/4/