Redactor API

destroy

Destroy Redactor.

To call internally from a plugin:

this.destroy();

External call:

$('#redactor').redactor('destroy');
getObject

This method returns Redactor's object.

External call:

var redactor_object = $('#redactor').redactor('getObject');

To call internally from a plugin use this.

getEditor

This method returns Redactor's editable layer.

External call:

var redactor_editor = $('#redactor').redactor('getEditor');

To call internally from a plugin use this.$editor.

getBox

This method returns Redactor's container layer.

External call:

var redactor_box = $('#redactor').redactor('getBox');

To call internally from a plugin use this.$box.

getIframe

This method returns Redactor's iframe if editor in iframe mode, if there is no returns false.

External call:

var redactor_iframe = $('#redactor').redactor('getIframe');

To call internally from a plugin use this.$frame.

getToolbar

This method returns Redactor's toolbar layer.

External call:

var redactor_toolbar = $('#redactor').redactor('getToolbar');

To call internally from a plugin use this.$toolbar.

bufferSet

Before you insert something into the editor, you can turn on the buffer. In this case, Undo will work.

To call internally from a plugin:

this.bufferSet();

External call:

$('#redactor').redactor('bufferSet');
observeImages

Launch an event, which listens to inserted images and allows to edit and scale them:

To call internally from a plugin:

this.observeImages();

External call:

$('#redactor').redactor('observeImages');
observeTables

Launch an event that listens to inserted tables and allows to edit them.

To call internally from a plugin:

this.observeTables();

External call:

$('#redactor').redactor('observeTables');
observeStart

Simultaneous launch of observeImages and observeTables

To call internally from a plugin:

this.observeStart();

External call:

$('#redactor').redactor('observeStart');
buttonGet

Get a button object by its key.

To call internally from a plugin:

var button = this.buttonGet('bold');

External call:

var button = $('#redactor').redactor('buttonGet', 'bold');
buttonAdd

Append a button at the end of the toolbar.

To call internally from a plugin:

var btn = this.buttonAdd('newbutton', 'Title of button',
function(buttonName, buttonDOM, buttonObj, e)
{
	// callback function
});

External call:

$('#redactor').redactor('buttonAdd', 'newbutton', 'Title of button',
function(buttonName, buttonDOM, buttonObj, e)
{
	// callback function
});
buttonAddFirst

Append a button at the beginning of the toolbar.

buttonAddAfter

Add a button before another button.

To call internally from a plugin:

var btn = this.buttonAddAfter('bold', 'newbutton', 'Title of button',
function(buttonName, buttonDOM, buttonObj, e)
{
    // callback function
});

External call:

$('#redactor').redactor('buttonAddAfter', 'bold', 'newbutton', 'Title of button',
function(buttonName, buttonDOM, buttonObj, e)
{
    // callback function
});
buttonAddBefore

Add a button after another button.

buttonRemove

Delete button.

To call internally from a plugin:

this.buttonRemove('bold');

External call:

$('#redactor').redactor('buttonRemove', 'bold');
buttonActive

Make a button active.

To call internally from a plugin:

this.buttonActive('bold');

External call:

$('#redactor').redactor('buttonActive', 'bold');
buttonInactive

Make a button inactive.

To call internally from a plugin:

this.buttonInactive('bold');

External call:

$('#redactor').redactor('buttonInactive', 'bold');
buttonInactiveAll

Make all the buttons inactive.

To call internally from a plugin:

this.buttonInactiveAll();

External call:

$('#redactor').redactor('buttonInactiveAll');
buttonAwesome

Set button as Font Awesome's icon.

First you need connect Font Awesome on your webpage:

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">

After you can set a plugin's button as Font Awesome's icon:

this.buttonAdd('plugin-button', 'My plugin', this.myPluginMethod);
this.buttonAwesome('plugin-button', 'fa-tasks');

You can see names of buttons for Font Awesome here: http://fontawesome.io/icons/.

buttonTagToActiveState

Setting the active state (behavior) for added buttons.

To call internally from a plugin:

this.buttonTagToActiveState('button', 'pre');

// 'button' - name of added button
// 'pre' - tag on which button will take an active state
focus

Placing focus at the beginning of the text.

To call internally from a plugin:

this.focus();

External call:

$('#redactor').redactor('focus');
focusEnd

Placing focus at the end of the text.

To call internally from a plugin:

this.focusEnd();

External call:

$('#redactor').redactor('focusEnd');
toggle

Switch between visual and code modes

To call internally from a plugin:

this.toggle();

External call:

$('#redactor').redactor('toggle');
sync

Sync code. This method gets the code from a visual layer and puts it into a textarea using code.get method.

To call internally from a plugin:

this.sync();

External call:

$('#redactor').redactor('sync');
get

Get code. This method calls automatically clean.stripTags method to clean-up the code.

To call internally from a plugin:

var html = this.get();

External call:

var html = $('#redactor').redactor('get');
set

Sets the code.

To call internally from a plugin:

this.set('your code');

External call:

$('#redactor').redactor('set', 'your code');

cleanStripTags method is called automatically on setting the code. If you wish to turn it off, set it in parameters:

this.set('your code', false);
$('#redactor').redactor('set', 'your code', false);
insertHtml

Insert code.

To call internally from a plugin:

this.insertHtml('your code');

External call:

$('#redactor').redactor('insertHtml', 'your code');
insertText

Insert text.

To call internally from a plugin:

this.insertText('your text or code');

External call:

$('#redactor').redactor('insertText', 'your text or code');
insertHtmlAdvanced

Special code insert. If you know that the code will content block elements, use this method to insert such code exactly as it is.

To call internally from a plugin:

this.insertHtmlAdvanced('your code');

External call:

$('#redactor').redactor('insertHtmlAdvanced', 'your code');
insertNode

Insert element berfore cursor. It is different form insert.beforeCaret and insert.afterCaret: if the text was selected, this text will be replace by inserted one.

To call internally from a plugin:

var node = $('<span>inserted</span>');
this.insertNode(node);

External call:

var node = $('<span>inserted</span>');
$('#redactor').redactor('insertNode', node);
cleanParagraphy

Code formatting with paragraphs.

To call internally from a plugin:

var html = this.cleanParagraphy(html);

External call:

var html = $('#redactor').redactor('cleanParagraphy', html);
cleanStripTags

Code clean-up according to Redactor settings. By default, it always cleans up tags from clearTags array It there are specific settings in allowedTags or deniedTags, the code will be cleaned up according to this settings.

To call internally from a plugin:

var html = this.cleanStripTags(html);

External call:

var html = $('#redactor').redactor('cleanStripTags', html);
cleanSavePreCode

If you are performing a clean-up, it is a good idea to run clean.savePreCode first to preserve pre tag formatting.

To call internally from a plugin:

var html = this.cleanSavePreCode(html);

External call:

var html = $('#redactor').redactor('cleanSavePreCode', html);
execCommand

A method to call commands of document.execCommand. After execution this method will automatically sync code using this.sync();

To call internally from a plugin:

this.execCommand('formatblock', 'h2');

External call:

$('#redactor').redactor('execCommand', 'formatblock', 'h2');
formatQuote

Transform text into a blockquote and vice versa.

To call internally from a plugin:

this.formatQuote();

External call:

$('#redactor').redactor('formatQuote');
formatBlocks

Transform selected block elements into other block elements.

To call internally from a plugin:

this.formatBlocks('h2');

External call:

$('#redactor').redactor('formatBlocks', 'h2');
blockSetAttr

Set attribute for a block element.

To call internally from a plugin:

this.blockSetAttr('title', 'My title');

External call:

$('#redactor').redactor('blockSetAttr', 'title', 'My title');
blockRemoveAttr

Delete attribute from a block element

To call internally from a plugin:

this.blockRemoveAttr('title');

External call:

$('#redactor').redactor('blockRemoveAttr', 'title');
blockSetStyle

Set style to a block element.

To call internally from a plugin:

this.blockSetStyle('font-size', '30px');

External call:

$('#redactor').redactor('blockSetStyle', 'font-size', '30px');
blockRemoveStyle

Delete style from a block element.

To call internally from a plugin:

this.blockRemoveStyle('font-size');

External call:

$('#redactor').redactor('blockRemoveStyle', 'font-size');
blockSetClass

Set class to a block element.

To call internally from a plugin:

this.blockSetClass('my_class');

External call:

$('#redactor').redactor('blockSetClass', 'my_class');
blockRemoveClass

Delete class from a block element.

To call internally from a plugin:

this.blockRemoveClass('my_class');

External call:

$('#redactor').redactor('blockRemoveClass', 'my_class');
inlineSetAttr

Set attribute for selected text of inline element.

To call internally from a plugin:

this.inlineSetAttr('title', 'My title');

External call:

$('#redactor').redactor('inlineSetAttr', 'title', 'My title');
inlineRemoveAttr

Delete attribute from selected text of inline element.

To call internally from a plugin:

this.inlineRemoveAttr('title');

External call:

$('#redactor').redactor('inlineRemoveAttr', 'title');
inlineSetStyle

Set style for selected text of inline element.

To call internally from a plugin:

this.inlineSetStyle('font-size', '30px');

External call:

$('#redactor').redactor('inlineSetStyle', 'font-size', '30px');
inlineRemoveStyle

Remove style from selected text of inline element.

To call internally from a plugin:

this.inlineRemoveStyle('font-size');

External call:

$('#redactor').redactor('inlineRemoveStyle', 'font-size');
inlineSetClass

Set class for selected text of inline element.

To call internally from a plugin:

this.inlineSetClass('my_class');

External call:

$('#redactor').redactor('inlineSetClass', 'my_class');
inlineRemoveClass

Remove class from selected text of inline element.

To call internally from a plugin:

this.inlineRemoveClass('my_class');

External call:

$('#redactor').redactor('inlineRemoveClass', 'my_class');
inlineFormat

Transform selected text or inline element into another element.

To call internally from a plugin:

this.inlineFormat('em');

External call:

$('#redactor').redactor('inlineFormat', 'em');
inlineRemoveFormat

Remove inline element from selected text.

To call internally from a plugin:

this.inlineRemoveFormat('em');

External call:

$('#redactor').redactor('inlineRemoveFormat', 'em');
selectionSave

Save cursor position.

To call internally from a plugin:

this.selectionSave();

External call:

$('#redactor').redactor('selectionSave');
selectionRestore

Restore cursor position.

To call internally from a plugin:

this.selectionRestore();

External call:

$('#redactor').redactor('selectionRestore');
selectionRemoveMarkers

Remove selection save's markers.

To call internally from a plugin:

this.selectionRemoveMarkers();

External call:

$('#redactor').redactor('selectionRemoveMarkers');
selectionAll

Select all text.

To call internally from a plugin:

this.selectionAll();

External call:

$('#redactor').redactor('selectionAll');
getCaretOffset

Get cursor position in the current element.

To call internally from a plugin:

var offset = this.getCaretOffset(element);

External call:

var offset = $('#redactor').redactor('getCaretOffset', element);
setCaret

Set cursor position to a specific position of an element.

To call internally from a plugin:

this.setCaret(element, 4);

External call:

$('#redactor').redactor('setCaret', element, 4);
selectionElement

Select element.

To call internally from a plugin:

this.selectionElement(element);

External call:

$('#redactor').redactor('selectionElement', element);
selectionStart

Set cursor to the beginning of an element

To call internally from a plugin:

this.selectionStart(element);

External call:

$('#redactor').redactor('selectionStart', element);
selectionEnd

Set cursor to the end of an element

To call internally from a plugin:

this.selectionEnd(element);

External call:

$('#redactor').redactor('selectionEnd', element);
getCurrent

Get current element (where cursor is right now)

To call internally from a plugin:

var current = this.getCurrent();

External call:

var current = $('#redactor').redactor('getCurrent');
getParent

Get parent element of the cursor.

To call internally from a plugin:

var parent = this.getParent();

External call:

var parent = $('#redactor').redactor('getParent');
getBlock

Get current block element (where cursor is right now).

To call internally from a plugin:

var block = this.getBlock();

External call:

var block = $('#redactor').redactor('getBlock');
getBlocks

Get all selected block elements.

To call internally from a plugin:

var blocks = this.getBlocks();

External call:

var blocks = $('#redactor').redactor('getBlocks');
getNodes

Get all selected elements.

To call internally from a plugin:

var nodes = this.getNodes();

External call:

var nodes = $('#redactor').redactor('getNodes');
getSelectionText

Get plain text of selected text.

To call internally from a plugin:

var text = this.getSelectionText();

External call:

var text = $('#redactor').redactor('getSelectionText');
getSelectionHtml

Get HTML of selected text.

To call internally from a plugin:

var html = this.getSelectionHtml();

External call:

var html = $('#redactor').redactor('getSelectionHtml');
selectionRemove

Remove selection

To call internally from a plugin:

this.selectionRemove();

External call:

$('#redactor').redactor('selectionRemove');
setCaretAfter

Set cursor after an element

To call internally from a plugin:

this.setCaretAfter(element);

External call:

$('#redactor').redactor('setCaretAfter', element);
modalInit

Call a modal window.

somefunction: function()
{
    // callback (optional)
    var callback = $.proxy(function()
    {
    	 // save cursor position
       this.selectionSave();

       // some code

    }, this);

    // call a modal from element with ID #mymodal
    this.modalInit('Title of Modal', '#mymodal', 500, callback);
    // 500 it is the width of the window in pixels

    // or call a modal with a code
    this.modalInit('Title of Modal', '<p>Your modal code</p>', 500, callback);
}
modalClose

Close your modal window (all modal windows).

To call internally from a plugin:

this.modalClose();

External call:

$('#redactor').redactor('modalClose');
uploadInit

Ajax file upload.

// set field
<input type="file" name="file_name">

// call upload.init with onchange event
somefunction: function()
{
    this.uploadInit('file_name',
    {
        auto: true,
        url: 'your_upload_url',
        success: $.proxy(uploadCallbackFunction, this)
    });
}
draguploadInit

Drag and drop upload.

// set field
<input type="file" id="file_id" name="file">

// call dragupload
somefunction: function()
{
    this.draguploadInit('#file_id',
    {
        url: 'your_upload_url',
        success: $.proxy(function(data)
        {
            fileUploadCallbackFunction(data);
        }, this)
    });
}
browser

Detect web-browser ('webkit', 'mozilla', 'opera', 'msie').

To call internally from a plugin:

if (this.browser('msie')) // return true or false
{
    // some code
}
outerHtml

Get the element as HTML code.

To call internally from a plugin:

var html = this.outerHtml(element);

External call:

var html = $('#redactor').redactor('outerHtml', element);
normalize

Remove 'px' from string.

To call internally from a plugin:

var value = this.normalize('12px'); // return 12 as Number

External call:

var value = $('#redactor').redactor('normalize', '12px');
isParentRedactor

Determine if an element is inside of Redactor.

var isredactor = this.isParentRedactor(element); // return true or false

External call:

var isredactor = $('#redactor').redactor('isParentRedactor', element);
isMobile

Detect mobile devices.

To call internally from a plugin:

var ismobile = this.isMobile(); // return true or false

External call:

var ismobile = $('#redactor').redactor('isMobile');
showProgressBar

This method show Redactor's progress bar.

External call:

$('#redactor').redactor('showProgressBar');

To call internally from a plugin use this.showProgressBar();.

hideProgressBar

This method hide Redactor's progress bar.

External call:

$('#redactor').redactor('hideProgressBar');

To call internally from a plugin use this.hideProgressBar();.