Git Product home page Git Product logo

jquery-textrange's Introduction

This jQuery plugin is for easily finding the starting and ending positions of selected text in text fields and textareas. It can also be used to set the selection, and replace the selection with given text (or insert text wherever the cursor is in the text field or textarea).

Demo

Browser Support

  • Chrome
  • Firefox
  • Microsoft Edge
  • yes, even Internet Explorer 5.5+

Include

Include the file directly using <script> tags:

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="jquery-plugins/jquery-textrange.js"></script>

๐Ÿ“ note: jquery-textrange can be loaded through any UMD-compatible Javascript Module Loader.

Methods

You can use this method to get all the information on the selected text of an element or a specific bit of information.

'get'

Get everything
$('input[name="example"]').textrange('get');

or for short:

$('input[name="example"]').textrange();

This will return a JSON object with the following information:

{
   "position": // cursor location in the text field)
   "start": //starting position of the selected text in the text field
   "end": // ending position of the selected text in the text field
   "length": // the length of the selected text in the text field
   "text": // the text that is selected
}
Get a particular property

This function can also be used to get a particular property of the object.

For example, this will obtain the starting location of the selected text:

var start = $('input[name="example"]').textrange('get', 'start');

'set'

You can use this method to set the starting and ending locations of the selected text in an element.

It works much like PHP's substr(), so if you're familiar with that, it should be a breeze! Here are some examples, anyway.

For the following examples, let's say input[name="example"] contains the text abcdef.

$('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
$('input[name="example"]').textrange('set', 2); // selects "cdef"
$('input[name="example"]').textrange('set', 2, 3); // selects "cde"
$('input[name="example"]').textrange('set', 2, -2); // selects "cd"
$('input[name="example"]').textrange('set', -3); // selects "def"
$('input[name="example"]').textrange('set', -2, 1); // selects "e"
$('input[name="example"]').textrange('set', -4, -1); // selects "cde"

If you're looking to set the cursor at one specific location, you can use 0 for length, or you can use $().textrange('setcursor') (see below).

'setcursor'

You can use this method to set the location of the cursor in your text field.

To set the cursor at the fifth character position:

$('input[name="example"]').textrange('setcursor', 5);

'replace'

You can use this method to replace the selection with given text.

$('input[name="example"]').textrange('replace', 'some text');

There is also an insert alias for replace if you're using this method to insert text at the cursor location. They work the same way.

Options

For the first parameter of each method, you can pass an object instead of a string with additional options:

$('input[name="example"]').textrange({ method: 'get' });
$('input[name="example"]').textrange({ method: 'set', nofocus: true }, 2);
$('input[name="example"]').textrange({ method: 'get', nofocus: true }, 'start');
  • method (defaults to 'get'): One of the methods above.
  • nofocus: Do not call .focus() on the dom element. See PR #20.

jquery-textrange's People

Contributors

codef0rmerz avatar dwittner avatar imhoffd 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  avatar

jquery-textrange's Issues

Undo/Redo Issue

Undo/Redo stops working after doing an insert, is there a way around this?

I have a textarea where text gets inserted into when a button is pressed via textrange but after the initial insert/replace, undo/redo no longer works on that textarea.

ReplaceAt method

First of all, thanks for this plugin, really useful!

Now I will suggest a feature that can help a lot of people. Now we can get the text between two positions, but we don't have a method to replace this.

Okay, have "replace", but this replaces based on text. Sometimes developers need replace text by position, like:

"The text between positions 3 and 10 will be replaced with 'foobar' ".

Understand what I want mean? My feature suggestion is that, make a method to replace at two given positions, like this:

replaceAt(startPosition, endPosition, newValue)

What you think?

Compatibility with TinyMCE 3?

I'm using TinyMCE for a text area and I'm trying to grab the selected text within the TinyMCE editor to modify. My var range = $("#editor").textrange(); code keeps returning an object but the position, start, end and length are all 0 and text is blank.

IE8 textrange('get', 'text') returns '[object Object]'

In IE8 textrange('get', 'text') returns '[object Object]' rather than text, I believe this is because 'get' doesn't return the individual props.

return {
    position: rangetextcopy.text.length,
    start: rangetextcopy.text.length,
    end: rangetextcopy.text.length + range.text.length,
    length: range.text.length,
    text: range.text
};

should be

var props =  {
    position: rangetextcopy.text.length,
    start: rangetextcopy.text.length,
    end: rangetextcopy.text.length + range.text.length,
    length: range.text.length,
    text: range.text
};
return typeof property === 'undefined' ? props : props[property];

publish to npm

Hello,

Would like to use this great looking plugin with browserify / cartero. Looks like you already have a UMD.. would you be so kind as to add package.json and publish to npm? Can do a PR if you'd like.

Thx!

David

Start value in IE8 is not correct

My textarea has content as below:

line 1
[new line]
[new line]
line 4

When I select "line 4", start value is 6. But it should be 12 (because in ie8 CRLF is line break).

Is it possible to select the current word when click on word in textarea

Thanks for useful plugin!
I want to select whole word when I click on specific word. is it possible?

I have list of tags in my textarea content like <first_name>, etc. When user clicks on the tag. I want show the list of available to tags to the user. so when user selects the the tag from the list I simply want to replace. I can do most of the thinks using your plugin. But I don't know how to select the whole word when user simply clicks on the word in textarea.

Thanks

Don't focus input on hover

Currently, any input or textarea making use of textrange becomes focused whenever it is hovered over. Can this be prevented?

Extend functionality to other DOM elements

Investigate and allow get/set/replace functionality on any DOM element (beyond textareas and text inputs).

Investigate a new "replaceRange" method that would not require selecting text in order to replace text. (Currently replace simply replaces selected text.)

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.