Do things to the selection in a <textarea>
, or a textarea-like editable element.
Provided by the jquery.textSelection
ResourceLoader module.
Example
mw.loader.using( 'jquery.textSelection' ).then( () => {
const contents = $( '#wpTextbox1' ).textSelection( 'getContents' );
} );
- Source:
Methods
$.fn.textSelection(command, [commandOptions]) → {any}static
#
Execute a textSelection command about the element.
Example
var $textbox = $( '#wpTextbox1' );
$textbox.textSelection( 'setContents', 'This is bold!' );
$textbox.textSelection( 'setSelection', { start: 8, end: 12 } );
$textbox.textSelection( 'encapsulateSelection', { pre: '<b>', post: '</b>' } );
// Result: Textbox contains 'This is <b>bold</b>!', with cursor before the '!'
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
command |
string | Command to execute, one of: |
|
commandOptions |
any |
optional |
Options to pass to the command |
- Source:
Returns:
Depending on the command
- Type
- any
encapsulateSelection([options]) → {jQuery}staticchainable
#
Insert text at the beginning and end of a text selection, optionally inserting text at the caret when selection is empty.
Also focusses the textarea.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
optional |
Properties:
|
- Source:
Returns:
- Type
- jQuery
Insert text at the beginning and end of a text selection, optionally inserting text at the caret when selection is empty.
getCaretPosition([options]) → {number|Array.<number>}static
#
Get the current cursor position (in UTF-16 code units) in a textarea.
Parameters:
Name | Type | Attributes | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
optional |
Properties:
|
- Source:
Returns:
- When
startAndEnd
isfalse
: number - When
startAndEnd
istrue
: array with two numbers, for start and end of selection
getSelection() → {string}static
#
register(functions)static
#
Register an alternative textSelection API for this element.
Parameters:
Name | Type | Description |
---|---|---|
functions |
Object | Functions to replace. Keys are command names (as in |
- Source:
replaceSelection(value) → {jQuery}staticchainable
#
scrollToCaretPosition([options]) → {jQuery}staticchainable
#
Scroll a textarea to the current cursor position. You can set the cursor
position with setSelection
.
Parameters:
Name | Type | Attributes | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
optional |
Properties:
|
- Source:
Returns:
- Type
- jQuery