├── LICENSE
├── README.md
├── bower.json
├── demo.html
├── jquery-textrange.js
└── package.json
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 Daniel Imhoff
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of
4 | this software and associated documentation files (the "Software"), to deal in
5 | the Software without restriction, including without limitation the rights to
6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 | the Software, and to permit persons to whom the Software is furnished to do so,
8 | subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This jQuery plugin is for easily finding the starting and ending positions of
2 | selected text in text fields and textareas. It can also be used to set the
3 | selection, and replace the selection with given text (or insert text wherever
4 | the cursor is in the text field or textarea).
5 |
6 | * jquery-textrange may not work with WYSIWYG editors. See [issue
7 | #3](https://github.com/dwieeb/jquery-textrange/issues/3) and [this
8 | question](https://wordpress.stackexchange.com/questions/105961/insert-text-a-cursor-position-in-to-tinymce-text-editor).
9 |
10 | ## Demo
11 |
12 | * https://dwieeb.github.com/jquery-textrange/
13 |
14 | ## Browser Support
15 |
16 | * Chrome
17 | * Firefox
18 | * Microsoft Edge
19 | * *yes, even* Internet Explorer 5.5+
20 |
21 | ## Include
22 |
23 | Include the file directly using `
27 |
28 | ```
29 |
30 | :memo: *note*: jquery-textrange can be loaded through any
31 | [UMD](https://github.com/umdjs/umd/blob/master/README.md)-compatible Javascript
32 | Module Loader.
33 |
34 | ## Methods
35 |
36 | You can use this method to get all the information on the selected text of an
37 | element or a specific bit of information.
38 |
39 | ### 'get'
40 |
41 | ##### Get everything
42 |
43 | ```javascript
44 | $('input[name="example"]').textrange('get');
45 | ```
46 |
47 | or for short:
48 |
49 | ```javascript
50 | $('input[name="example"]').textrange();
51 | ```
52 |
53 | This will return a JSON object with the following information:
54 |
55 | ```javascript
56 | {
57 | "position": // cursor location in the text field)
58 | "start": //starting position of the selected text in the text field
59 | "end": // ending position of the selected text in the text field
60 | "length": // the length of the selected text in the text field
61 | "text": // the text that is selected
62 | }
63 | ```
64 |
65 | ##### Get a particular property
66 |
67 | This function can also be used to get a particular property of the object.
68 |
69 | For example, this will obtain the starting location of the selected text:
70 |
71 | ```javascript
72 | var start = $('input[name="example"]').textrange('get', 'start');
73 | ```
74 |
75 | ### 'set'
76 |
77 | You can use this method to set the starting and ending locations of the
78 | selected text in an element.
79 |
80 | It works much like [PHP's
81 | substr()](https://secure.php.net/manual/en/function.substr.php), so if you're
82 | familiar with that, it should be a breeze! Here are some examples, anyway.
83 |
84 | For the following examples, let's say `input[name="example"]` contains the text
85 | `abcdef`.
86 |
87 | ```javascript
88 | $('input[name="example"]').textrange('set'); // selects "abcdef" (select all)
89 | $('input[name="example"]').textrange('set', 2); // selects "cdef"
90 | $('input[name="example"]').textrange('set', 2, 3); // selects "cde"
91 | $('input[name="example"]').textrange('set', 2, -2); // selects "cd"
92 | $('input[name="example"]').textrange('set', -3); // selects "def"
93 | $('input[name="example"]').textrange('set', -2, 1); // selects "e"
94 | $('input[name="example"]').textrange('set', -4, -1); // selects "cde"
95 | ```
96 |
97 | If you're looking to set the cursor at one specific location, you can use `0`
98 | for length, or you can use `$().textrange('setcursor')` (see below).
99 |
100 | ### 'setcursor'
101 |
102 | You can use this method to set the location of the cursor in your text field.
103 |
104 | To set the cursor at the fifth character position:
105 |
106 | ```javascript
107 | $('input[name="example"]').textrange('setcursor', 5);
108 | ```
109 |
110 | ### 'replace'
111 |
112 | You can use this method to replace the selection with given text.
113 |
114 | ```javascript
115 | $('input[name="example"]').textrange('replace', 'some text');
116 | ```
117 |
118 | There is also an `insert` alias for `replace` if you're using this method to
119 | insert text at the cursor location. They work the same way.
120 |
121 | ## Options
122 |
123 | For the first parameter of each method, you can pass an object instead of a
124 | string with additional options:
125 |
126 | ```javascript
127 | $('input[name="example"]').textrange({ method: 'get' });
128 | $('input[name="example"]').textrange({ method: 'set', nofocus: true }, 2);
129 | $('input[name="example"]').textrange({ method: 'get', nofocus: true }, 'start');
130 | ```
131 |
132 | * `method` (defaults to `'get'`): One of the methods above.
133 | * `nofocus`: Do not call `.focus()` on the dom element. See [PR
134 | #20](https://github.com/dwieeb/jquery-textrange/pull/20).
135 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery-textrange",
3 | "version": "1.4.0",
4 | "main": "jquery-textrange.js",
5 | "dependencies": {
6 | "jquery": ">=1.3"
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |