Git Product home page Git Product logo

rangyinputs's Introduction

Rangy Inputs

A small jQuery plug-in for selection and caret manipulation within textareas and text inputs.

Demo

https://rawgit.com/timdown/rangyinputs/master/demo/demo.html

Bower Install

Bower users can install by running bower install rangyinputs --save

Example

Imagine a simple textarea such as:

<textarea id="test">Foo bar</textarea>

You can get the user's selection using

var sel = $("#test").getSelection();
console.log(sel.start + ", " + sel.end);

To select the word "bar":

$("#test").setSelection(4, 7);

Other methods are listed below. Example code refers to the example textarea above.

API

Rangy Inputs provides the following extensions to all jQuery objects wrapping a single text input or textarea.

Focus

In Internet Explorer, the element must have the focus for any of the following methods to work, which can be achieved by calling its focus() method of the element (or its jQuery object) before calling the method.


getSelection()

Returns an object representing the user selection within the text input or textarea element.

The object returned has the following properties:

  • start: The character index of the start position of the selection
  • end: The character index of the end position of the selection
  • length: The number of characters selected
  • text: The selected text

Example

$("#test").focus();
var sel = $("#test").getSelection();
alert(sel.start + ", " + sel.end);

setSelection(Number start[, Number end])

Selects the text within the text input or textarea element between the specified start and end character indices. If end is omitted, the selection is removed and the caret is placed at character index start.

Returns a reference to the original jQuery object for the element.

Examples

To select the word "bar":

$("#test").setSelection(4, 7);

To place the caret before "bar":

$("#test").setSelection(4);

collapseSelection(Boolean toStart)

Collapses the selection to an insertion point (caret) either at the start of the current selection if toStart is true or the end of the current selection otherwise.

Returns a reference to the original jQuery object for the element.

Example

To collapse the selection to the start:

$("#test").collapseSelection(true);

deleteText(Number start, Number end, Boolean moveSelection)

Deletes the text within the text input or textarea element between the specified start and end character indices and optionally places the caret at the position where the deleted text had been if moveSelection is true.

Returns a reference to the original jQuery object for the element.

Example

To delete the word "foo" from the example and place the caret where "foo" had been:

$("#test").deleteText(0, 3, true);

deleteSelectedText()

Deletes the currently selected text within the text input or textarea element and places the caret at the position where the deleted text had been.

Returns a reference to the original jQuery object for the element.

Example

$("#test").deleteSelectedText();

extractSelectedText()

Deletes the currently selected text within the text input or textarea element, places the caret at the position where the deleted text had been and returns the text that was deleted.

Example

var extracted = $("#test").extractSelectedText();
console.log(extracted);

insertText(String text, Number pos[, String selectionBehaviour])

Inserts the specified text at the specified character position within the text input or textarea element and optionally updates the selection depending on the value of selectionBehaviour. Possible values are:

  • "select": Selects the inserted text
  • "collapseToStart": Collapses the selection to a caret at the start of the inserted text
  • "collapseToEnd": Collapses the selection to a caret at the end of the inserted text

If no value is supplied for selectionBehaviour, the selection is not changed and left at the mercy of the browser (placing the caret at the start is not uncommon when the textarea's value is changed).

Returns a reference to the original jQuery object for the element.

Example

To insert the word "baz" between "foo" and "bar" and place the caret immediately after "baz":

$("#test").insertText(" baz", 3, "collapseToEnd");

replaceSelectedText(String text[, String selectionBehaviour])

Replaces the currently selected text in the text input or textarea element with the specified text and optionally updates the selection depending on the value of selectionBehaviour. Possible values are:

  • "select": Selects the inserted text
  • "collapseToStart": Collapses the selection to a caret at the start of the inserted text
  • "collapseToEnd": Collapses the selection to a caret at the end of the inserted text

If no value is supplied for selectionBehaviour, "collapseToEnd" is assumed.

Returns a reference to the original jQuery object for the element.

Examples

To replace the selection with the word "baz" (or insert "baz" at the the caret position if no text is selected):

$("#test").replaceSelectedText("baz");

To do the same thing but select "baz" afterwards:

$("#test").replaceSelectedText("baz", "select");

surroundSelectedText(String textBefore, String textAfter[, String selectionBehaviour])

Surrounds the currently selected text in the text input or textarea element with the specified pieces of text and optionally updates the selection depending on the value of selectionBehaviour. Possible values are:

  • "select": Selects the text that was surrounded
  • "collapseToStart": Collapses the selection to a caret at the start of the surrounded text
  • "collapseToEnd": Collapses the selection to a caret at the end of the surrounded text

If no value is supplied for selectionBehaviour, "select" is assumed.

Returns a reference to the original jQuery object for the element.

Example

To surround the selection with HTML <b> tags:

$("#test").surroundSelectedText("<b>", "</b>");

rangyinputs's People

Contributors

asgh avatar thebox193 avatar timdown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rangyinputs's Issues

Make getSelection()['start'] the default value for [Number Start]

It would be nice if the Start argument in insertText() and related functions was optional and defaulted to .getSelection()['start']

So one would not have to do

$('#foo').insertText('Bar', $('#foo').getSelection()['start']);

but simply

$('#foo').insertText('Bar');

to insert at current caret position.

Can I using this plugin for contenteditable div?

I am using editor and i want to be replace button in editor menu. so that i have problem that selection text paste as |%selected_text%|

Like that selected Text is that Dhaval
replaced by : |%Dhaval%|

remove this character second time replacement to selected text Dhaval remove this character.
http://jsfiddle.net/varsadadj/7gcr6/

please give me right solution for this issue. for a long time i have this problem in my application.

extractSelectedText() behaviour differs between normal and minified JS

With the minified JS, if you put the cursor in a <textarea>, and don't select anything, then issue $('textarea').extractSelectedText() in the browser console, it deletes the character before the cursor, as if you pressed backspace.

The -src.js file doesn't behave this way. The version of both files is allegedly 1.2.0.

IE 8 Bug

Firstly, thanks this awesome tool helping my day.
Here is a bug report, hopes it can help others.

In IE 8, the return object of $().getSelection().text is buggy.
I found out the reason.
The reason is the new-line symbol in IE8 is \r\n and IE11 or FF or Chrome is \n
This makes setSelection() and getSelection() buggy.

/*
<textarea id="area"> a
b</textarea>
*/
$("#area").setSelection(1,3);
$("#area").getSelection();

Thanks.

Convert to NPM module?

Would be useful to have this published as an NPM module. It's convenient when using webpack and not having to rely on bower.

IE 9 Bug

I love this library and it works great in across all browsers, except I found an IE 9 bug in surroundSelectedText(). I am testing with IE 9.0.8112.16421 in Windows 7; the version shipped in the modern.ie Windows 7 IE 9 virtual machine.

If I have the following text in a <textarea> let's wrap this text OK? and I select this text from left-to-right with the mouse, I get let's wrap <strong></strong>this text OK? with the following function call.

function stabButtonWrapSimple(event) {
    var tag = event.data.tag;

    // Drupal.settings.stab.textareaId is the id including # of the element.
    $(Drupal.settings.stab.textareaId).focus();

    // Wrap tag around selected text.
    $(Drupal.settings.stab.textareaId).surroundSelectedText("<" + tag + ">", "</" + tag + ">");    
}

Is this something you would / could address? I am using this code in production and haven't been able to find a workaround.

Undo / redo support

I'm using this lovely little library to try to replace CodeMirror in the Ghost Editor with a plain textarea and I stumbled across the fact that making changes to a textarea with rangyinputs doesn't work with the browsers internal undo/redo.

I know that this is far from a simple problem to solve, and I'm also aware that you probably know far more about it than I do, seeing as researching the problem turned up this ๐Ÿ˜

As the answer to the question "can this be solved?" is "sort of", I was wondering if having support in rangyinputs would be welcomed either by default, or via an option?

IE8 and IE9 support

I tried to set the focus to an input element and used insertText ( with behavior as carettoend) to add a character the same text field. As per the document, I assume that the cursor position will be updated and it was visible though. For next insert, I tried to fetch the caret position (updated) using getSelection after setting focus() and proceed with next insertion. Every time it returns 0 and the insertion happens at 0th position only.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.