5

Can anyone suggest a plugin which can handle text selections in text areas (cross browser)?

I'm looking for a plugin which can:

  • Get current selection
  • Replace current selection
  • Select a range

(IE uses createTextRange while other browsers use setSelectionRange. There ought to exist an plugin which can do the above so that I don't have to reinvent the wheel.)

jgauffin
  • 99,844
  • 45
  • 235
  • 372

5 Answers5

5

jQuery Fieldselection: https://github.com/localhost/jquery-fieldselection

See: https://github.com/localhost/jquery-fieldselection/blob/master/jquery-fieldselection.js

Implements: getSelection, replaceSelection

Does not implement: currentSelection

Rangy: Implements: getSelection, ReplaceSelection, CurrentSelection

(note: not a jquery plugin, but widely applicable still)

A cross-browser JavaScript range and selection library. It provides a simple standards-based API for performing common DOM Range and Selection tasks in all major browsers, abstracting away the wildly different implementations of this functionality between Internet Explorer and DOM-compliant browsers.

Seems to implement what you want, for demo's see: http://rangy.googlecode.com/svn/trunk/demos/core.html

http://code.google.com/p/rangy/

See also: Jquery: selectionStart for non textarea elements

Rangyinputs A jquery plugin that offers similar functionality as Rangy, except for inputs instead of arbitrairy DOM elements.

A small cross-browser JavaScript library for obtaining and manipulating selections within and HTML elements.

Demo: http://rangyinputs.googlecode.com/svn/trunk/demos/textinputs_jquery.html

Project: http://code.google.com/p/rangyinputs/

Community
  • 1
  • 1
Arend
  • 3,741
  • 2
  • 27
  • 37
0

I tried many of the links present here, and others from the jQuery plugin repository, and none of them worked the way I wanted.

But there is this one, not mentionned here, that is really interesting :

http://madapaja.github.io/jquery.selection/

Hope it'll helps ! :)

Cyril N.
  • 38,875
  • 36
  • 142
  • 243
0

http://codepen.io/mattsich/pen/MKvmxQ for an iOS style selection I built. Ends up being just this:

$(document).ready(function(){
  $(".full-text").selectBars('.full-text', 'ipsum', function(){
    $('.selected-text p').text($('.full-text').attr('data-selected'));
  });

});
Matt Sich
  • 3,905
  • 1
  • 22
  • 26
0

Below plugin may help you http://www.examplet.buss.hk/jquery/caret.php

It was helpful too me

Nimit Dudani
  • 4,840
  • 3
  • 31
  • 46
0

I created my own. Only 2.6kb uncompressed: http://blog.gauffin.org/2012/02/a-javascript-selection-script/

//jQuery is not required but supported.
var selection = new textSelector($('#mytextarea'));
selection.replace('New text');

// you can change selection:
selection.select(1,10); // select char 1 to 10

// get selection
console.log("Start char: " + selection.get().start);

// check if anything is selected
selection.isSelected();

// get the text
var text = selection.text();

It's available at github: https://raw.github.com/jgauffin/griffin.editor/master/Source/textselector.js

jgauffin
  • 99,844
  • 45
  • 235
  • 372