Documentation

Selection

is

Returns: Boolean

Check if the selection is in Redactor.

var is = this.selection.is();

isCollapsed

Returns: Boolean

Check if the selection is collapsed or in other words, Redactor has a focus, and the caret is blinking, but the text is not selected.

var is = this.selection.isCollapsed();

isBackwards

Returns: Boolean

Check if the selection has started from right to left.

var is = this.selection.isBackwards();

isIn

Arguments
el Node Element Dom

Returns: Boolean

Check if the element is in the selection.

var is = this.selection.isIn(element);

isText

Returns: Boolean

Check if the caret is in a text:

  • text has not parents blocks
  • text is in a table cell and has not parents blocks
var is = this.selection.isText();

isAll

Arguments
el (optional) Node Element Dom

Returns: Boolean

Check if all content of the editor or the element is selected.

//  the editor
var is = this.selection.isAll();

// the element
var is = this.selection.isAll(element);

setRange

Arguments
range Object

Update the selection by the specified range object.

var node = document.createTextNode('My node');
var range = this.selection.getRange();
range.collapse(true);
range.insertNode(node);
range.setEndBefore(node);
this.selection.setRange(range);

setAll

Arguments
el (optional) Node Element Dom

Select all content of the editor or the element.

// select all content of the editor
this.selection.setAll();

// select all content of the element
this.selection.setAll(element);

get

Returns: Object

Return the selection object.

var sel = this.selection.get();

getRange

Returns: Object

Return the range object.

var range = this.selection.getRange();

getPosition

Returns: Object Boolean

Return the left and the right position (in pixels) of the selection relative to the entire document. Return false if Redactor does not have the focus.

var pos = this.selection.getPosition();
console.log(pos.left, pos.top);

getTextBeforeCaret

Arguments
num (optional) Number

Returns: String Boolean

By default, this method returns the last char in the text before the caret, but if you pass a num argument, you can get the required number of characters before the caret.

If there are no chars before the caret returns false.

// get the last char
var text = this.selection.getTextBeforeCaret();

// get ten last chars before the caret
var text = this.selection.getTextBeforeCaret(10);

getTextAfterCaret

Arguments
num (optional) Number

Returns: String Boolean

By default, this method returns the first char in the text after the caret, but if you pass a num argument, you can get the required number of characters after the caret.

If there are no chars after the caret returns false.

// get the first char
var text = this.selection.getTextAfterCaret();

// get ten first chars after the caret
var text = this.selection.getTextAfterCaret(10);

getCurrent

Returns: Node Boolean

Return the current element relative to the caret. Return false if Redactor does not have the focus.

var node = this.selection.getCurrent();

getParent

Returns: Node Boolean

Return the parent node of the current element relative to the caret. Return false if Redactor does not have the focus.

var node = this.selection.getParent();

getElement

Arguments
el (optional) Node Element Dom

Returns: Node Boolean

Return the first element (not text node) relative to the caret or the specified element. Return false if Redactor does not have the focus.

var node = this.selection.getElement();
var node = this.selection.getElement(element);

getInline

Arguments
el (optional) Node Element Dom

Returns: Node Boolean

Return the parent inline tag relative to the caret position or the specified element. If the inline tag has nested inline tags, returns the first level inline. Return false if the parent inline tag is not found or Redactor does not have the focus.

var node = this.selection.getInline();
var node = this.selection.getInline(element);

getInlineFirst

Arguments
el (optional) Node Element Dom

Returns: Node Boolean

Return the last level inline tag relative to the caret position or the specified element. Return false if the inline tag is not found or Redactor does not have the focus.

var node = this.selection.getInlineFirst();
var node = this.selection.getInlineFirst(element);

getInlineAll

Arguments
el (optional) Node Element Dom

Returns: Array

Return the all parents inline tags relative to the caret position or the specified element.

var node = this.selection.getInlineAll();
var node = this.selection.getInlineAll(element);

getBlock

Returns: Node Boolean

Return the first block tag relative to the caret or the specified element. Return false if Redactor does not have the focus.

var node = this.selection.getBlock();
var node = this.selection.getBlock(element);

getBlocks

Arguments
options Object
tags Arrayan array of tags to be selected
first Booleanif true, then selects only the first level of block tags (the parent of the block tag is the editor)

Returns: Array

Return the array of the selected block tags.

var blocks = this.selection.getBlocks();
var blocks = this.selection.getBlocks({ tags: ['p', 'h1'] });
var blocks = this.selection.getBlocks({ first: true });

getInlines

Arguments
options Object
tags Arrayan array of tags to be selected
all Booleanif true, then selects all inline tags, not only the first level
inside Booleanif true, it selects tags that are only inside of the selection.

Returns: Array

Return the array of the selected inline tags.

var inlines = this.selection.getInlines();
var inlines = this.selection.getInlines({ tags: ['strong', 'em'] });
var inlines = this.selection.getInlines({ all: true });

getInlinesAllSelected

Arguments
options Object
tags Arrayan array of tags to be selected

Returns: Array

Return the array of inline tags which are selected entire only.

var inlines = this.selection.getInlinesAllSelected();
var inlines = this.selection.getInlinesAllSelected({ tags: ['a', 'span'] });

getElements

Arguments
options Object
tags Arrayan array of tags to be selected

Returns: Array

Return the array of the selected elements (not text nodes).

var elements = this.selection.getElements();
var elements = this.selection.getElements({ tags: ['p', 'span'] });

getNodes

Arguments
options Object
tags Arrayan array of tags to be selected
textnodes Booleanif false, then gets only block or inline tags (not text nodes)
keepbr Boolean
if true, then gets BR with texnodes true

Returns: Array

Return the array of the selected block, inline and text nodes.

var nodes = this.selection.getNodes();
var nodes = this.selection.getNodes({ tags: ['p', 'span'] });
var elements = this.selection.getNodes({ textnodes: false });

getText

Returns: String

Return the selection as a text string.

var text = this.selection.getText();

getHtml

Returns: String

Return the selection as a HTML string.

var html = this.selection.getHtml();

save

Save the current selection.

this.selection.save();

restore

Restore the current selection if it was saved before by the method selection.save.

this.selection.restore();

clear

Clear the current selection from the editor content, elements or components.

this.selection.clear();

collapseToStart

Set the caret at the beginning of the selection and collapse it.

this.selection.collapseToStart();

collapseToEnd

Set the caret at the end of the selection and collapse it.

this.selection.collapseToEnd();