Expand all

jquery.textSelection

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
Execute a textSelection command about the element.

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:
Name Type Attributes Default Description
pre string optional

Text to insert before the cursor/selection

peri string optional

Text to insert between pre and post and select afterwards

post string optional

Text to insert after the cursor/selection

ownline boolean optional
false

Put the inserted text on a line of its own

replace boolean optional
false

If there is a selection, replace it with peri instead of leaving it alone

selectPeri boolean optional
true

Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true)

splitlines boolean optional
false

If multiple lines are selected, encapsulate each line individually

selectionStart number optional

Position to start selection at

selectionEnd number optional
options.selectionStart

Position to end selection at

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:
Name Type Attributes Default Description
startAndEnd Object optional
false

Return range of the selection rather than just start

Source:

Returns:

  • When startAndEnd is false: number
  • When startAndEnd is true: array with two numbers, for start and end of selection
Type
number | Array.<number>
Get the current cursor position (in UTF-16 code units) in a textarea.

getContents() → {string}static #

Get the contents of the textarea.

Source:

Returns:

Type
string
Get the contents of the textarea.

getSelection() → {string}static #

Get the currently selected text in this textarea.

Source:

Returns:

Type
string
Get the currently selected text in this textarea.

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 textSelection, except 'register' and 'unregister'). Values are functions to execute when a given command is called.

Source:
Register an alternative textSelection API for this element.

replaceSelection(value) → {jQuery}staticchainable #

Replace the selected text in the textarea with the given text, or insert it at the cursor.

Parameters:

Name Type Description
value string
Source:

Returns:

Type
jQuery
Replace the selected text in the textarea with the given text, or insert it at the cursor.

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:
Name Type Attributes Default Description
force string optional
false

Whether to force a scroll even if the caret position is already visible.

Source:

Returns:

Type
jQuery
Scroll a textarea to the current cursor position.

setContents(content) → {jQuery}staticchainable #

Set the contents of the textarea, replacing anything that was there before.

Parameters:

Name Type Description
content string
Source:

Returns:

Type
jQuery
Set the contents of the textarea, replacing anything that was there before.

setSelection([options]) → {jQuery}staticchainable #

Set the current cursor position (in UTF-16 code units) in a textarea.

Parameters:

Name Type Attributes Description
options Object optional
Properties:
Name Type Attributes Default Description
start number
end number optional
options.start
Source:

Returns:

Type
jQuery
Set the current cursor position (in UTF-16 code units) in a textarea.

unregister()static #

Unregister the alternative textSelection API for this element (see register).

Source:
Unregister the alternative textSelection API for this element (see register).