This time I will share jQuery Plugin and tutorial about jQuery Plugin For Selecting and Replacing Text – textrange, hope it will help you in programming stack.
textrange is a jQuery text control plugin that allows you select a range of text in input field and textarea and replace the selected text with given text.
Installation:
2 |
$ npm install jquery-textrange |
5 |
$ bower install jquery-textrange |
How to use it:
1. Include jQuery library and textrange plugin on the page
2 |
< script src = "jquery-textrange.js" ></ script > |
2. Create a textarea with some more content
1 |
< textarea id = "textarea" name = "textarea" > |
3. Create some buttons to select and replace text in the textarea
01 |
< div >Select a substring starting at |
02 |
< input type = "text" id = "char-start" name = "char-start" value = "30" > |
04 |
< input type = "text" id = "char-end" name = "char-end" value = "55" > |
05 |
< input type = "button" id = "selection-set" name = "selection-set" value = "Go!!" > |
08 |
< div > Replace the selected text with: |
09 |
< input type = "text" id = "replace-text" name = "replace-text" value = "some text" > |
10 |
< input type = "button" id = "selection-replace" name = "selection-replace" value = "Go!!" > |
11 |
</ div >< br type = "_moz" > |
4. The javascript
02 |
$(document).ready( function () { |
04 |
var range = $( this ).textrange(); |
06 |
$( '#info .property' ).each( function () { |
07 |
if ( typeof range[$( this ).attr( 'id' )] !== 'undefined' ) { |
08 |
if ($( this ).attr( 'id' ) === 'text' ) { |
09 |
range[$( this ).attr( 'id' )] = range[$( this ).attr( 'id' )].replace(/n/g, "\n" ).replace(/r/g, "\r" ); |
12 |
$( this ).children( '.value' ).html(range[$( this ).attr( 'id' )]); |
18 |
$( '#selection-set' ).click( function () { |
19 |
$( '#textarea' ).textrange( 'set' , $( '#char-start' ).val(), $( '#char-end' ).val()).trigger( 'updateInfo' ).focus(); |
22 |
$( '#selection-replace' ).click( function () { |
23 |
$( '#textarea' ).textrange( 'replace' , $( '#replace-text' ).val()).trigger( 'updateInfo' ).focus(); |
Change logs:
v1.4.0 (2017-04-29)
- first arg can be string or object
2014-11-11
2014-08-13
2014-08-02
2013-12-08
- putting focus check that ran on all methods in entry method
This awesome jQuery plugin is developed by dwieeb. For more Advanced Usages, please check the demo page or visit the official website.