37

I'm trying to make my own WYSIWYG editor. Is there any way, how to get the text which has user selected in textarea?

For example, if user selects some word and then clicks button, how do I find out which text was selected?

I'm using jQuery.

Jakub Arnold
  • 79,807
  • 86
  • 218
  • 314

6 Answers6

77

window.getSelection().toString() worked for me with Chrome but not Firefox.

For obtaining the selected content in a <textarea> with Firefox:

function getSel() // javascript
{
    // obtain the object reference for the <textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    // obtain the selected text
    var sel = txtarea.value.substring(start, finish);
    // do something with the selected content
}

You could also use activeElement instead of getElementById.

Reference:

Honest Abe
  • 7,434
  • 4
  • 45
  • 59
  • 1
    `window.getSelection().toString()` does exactly the same thing, and you don't need to know the id of the textarea. You can't have multiple selections in different text areas at the same time. – Dan Dascalescu Sep 04 '15 at 11:24
  • 2
    @DanDascalescu I've tried that before, and now. It works with Chrome, but does not seem to work with FireFox. – Honest Abe Sep 04 '15 at 17:10
  • 1
    Correct - turns out [there is a Firefox bug filed in 2001](https://bugzilla.mozilla.org/show_bug.cgi?id=85686) about window.getSelection() not working in textareas! – Dan Dascalescu Sep 04 '15 at 23:58
  • Indeed, there's a bug in Firefox, which was filed back in 2001 (!). I've mentioned that in [my answer](http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript/32397146#32397146). – Dan Dascalescu Sep 05 '15 at 19:51
17

Handling selection is different for different browsers:

var userSelection;
if (window.getSelection) {
    userSelection = window.getSelection();
}
else if (document.selection) { // Opera
    userSelection = document.selection.createRange();
}

That gives you a range object. Each range represents a single selection (using control/command key it's possible to make multiple active selections).

The type of range object you have now depends on what browser. For IE it's a Text Range object; for others its a Selection object. To convert a Selection object into a text range, you can call getRangeAt; for Safari, you need to build that:

var range;
if (userSelection.getRangeAt)
    range = userSelection.getRangeAt(0);
else { // Safari
    range = document.createRange();
    range.setStart(userSelection .anchorNode, userSelection.anchorOffset);
    range.setEnd(userSelection.focusNode, userSelection.focusOffset);
}

The range object provides you with the starting and ending dom elements and text offset of the selection.

More information about range objects can be found at quirksmode.org here

If you are using jQuery, you may want to look at the light weight jQuery RTE Plugin by Batiste Bieler. It may do enough for your needs or at least give you something to start with.

Scott Evernden
  • 35,319
  • 14
  • 75
  • 83
9

Try the jquery-fieldselection plugin.

You can download it from here. There is an example too.

eKek0
  • 21,835
  • 24
  • 87
  • 117
9

getSelection()

This varies a bit by browser, see here for a function that alleges to work in most: http://snipplr.com/view/775/getselection/

tpdi
  • 32,745
  • 10
  • 74
  • 117
6

A small function that will get the selected string/text of a textarea or input element :

function getInputSelection(elem){
 if(typeof elem != "undefined"){
  s=elem[0].selectionStart;
  e=elem[0].selectionEnd;
  return elem.val().substring(s, e);
 }else{
  return '';
 }
}

Note that the above code needs jQuery for working. An example of getting the selected string of a input element with id abc123

getInputSelection($("#abc123"));

The above function will return the selected string. If there is no string that is selected by the user, it will return null.

More Examples

getInputSelection($("body").find("input[name=user]"));

On elements selected with class name, selected string of the first element of the array of elements is returned and not the selected string of all the elements. After all, selection in a page will always be 1.

Subin
  • 3,081
  • 1
  • 32
  • 63
-2
function getSel() {
var input = $("#text");
return input.html().toString().substring(getSelection().baseOffset,getSelection().extendOffset);
}
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
Mike
  • 1
  • 1