JavaScript/jQuery HtmlEditor - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

This tutorial explains how to add an HTML Editor to a page and configure its core features. The following control demonstrates the result:

Refer to the following sections for details on each configuration step. You can also find the full code in the GitHub repository.

View on GitHub

Create an HtmlEditor

The HTML Editor uses the DevExtreme Quill library. To create an HtmlEditor on your page, add DevExtreme to your application, reference the DevExtreme Quill script before the main DevExtreme script, and use the following code:

index.js
index.html
  • $(function () {
  • $("#html-editor").dxHtmlEditor({
  • // Configuration goes here
  • });
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  •  
  • <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/24.2.3/css/dx.light.css">
  • <link rel="stylesheet" href="index.css">
  • <script src="https://cdn3.devexpress.com/jslib/24.2.3/js/dx-quill.min.js"></script>
  • <script src="https://cdn3.devexpress.com/jslib/24.2.3/js/dx.all.js"></script>
  • <script src="index.js"></script>
  • </head>
  • <body class="dx-viewport">
  • <div id="html-editor">
  • <!-- Initial markup goes here -->
  • </div>
  • </body>
  • </html>

Set the Initial Value

The control stores the document markup in the value property. You can also declare HTML content inside the HtmlEditor element.

index.html
index.css
  • <html>
  • <head>
  • <!-- ... -->
  • </head>
  • <body class="dx-viewport">
  • <div id="html-editor">
  • <h2>
  • <img src="HtmlEditor.svg" alt="HTML Editor">
  • Rich Text Editor (HTML Editor)
  • </h2>
  • <br>
  • <p>DevExtreme JavaScript HTML Editor is a client-side WYSIWYG text editor that allows its users to format textual and visual content and store it as HTML.</p>
  • <p>Supported features:</p>
  • <ul>
  • <li>Inline formats:
  • <ul>
  • <li><strong>Bold</strong>, <em>italic</em>, <s>strikethrough</s> text formatting</li>
  • <li>Typeface, font size, text color changes (HTML only)</li>
  • </ul>
  • </li>
  • <li>Block formats:
  • <ul>
  • <li>Headers</li>
  • <li>Lists (ordered and unordered)</li>
  • <li>Code blocks</li>
  • <li>Quotes</li>
  • </ul>
  • </li>
  • <li>Built-in format customization</li>
  • <li>Mail Merge</li>
  • <li>Adaptive toolbar for working with images, links, and color formats</li>
  • <li>Copy-paste rich content (the control strips unsupported formats)</li>
  • <li>Embedded images specified as a link to an image file or as base64-encoded binary data</li>
  • <li>Mention (for example, @person)</li>
  • <li>Tables</li>
  • </ul>
  • <br>
  • <p>The editor supports the following frameworks and libraries:</p>
  • <table>
  • <tr>
  • <td><strong>jQuery</strong></td>
  • <td>v2.1 - v2.2 and v3.x</td>
  • </tr>
  • <tr>
  • <td><strong>Angular</strong></td>
  • <td>v7.0.x - v11.0.x</td>
  • </tr>
  • <tr>
  • <td><strong>React</strong></td>
  • <td>v16.2+</td>
  • </tr>
  • <tr>
  • <td><strong>Vue</strong></td>
  • <td>v2.6.3+</td>
  • </tr>
  • </table>
  • </div>
  • </body>
  • </html>
  • .dx-htmleditor-content img {
  • vertical-align: middle;
  • }
  •  
  • .dx-htmleditor-content table {
  • width: 50%;
  • }
  •  
  • .dx-htmleditor-content table td:last-child {
  • text-align: right;
  • }
NOTE

To update the value at the runtime, call the option(optionName, optionValue) method:

index.js
  • $(function () {
  • const editor = $("#html-editor").dxHtmlEditor({
  • valueType: "html"
  • }).dxHtmlEditor('instance');
  •  
  • editor.option("value", "<h4>This is the new html formatted content</h4>");
  • });

Configure the Toolbar

The HTML Editor's toolbar contains buttons and drop-down menus that allow users to edit and format content. To configure the toolbar, specify an array of items in the toolbar object. You can use predefined items or create custom items. You can specify a list of accepted values for certain predefined items, such as font size. To do so, use the name property to define the item and the acceptedValues property to assign an array of available values.

If toolbar items overflow the length of the toolbar, enable the multiline property to arrange the items in multiple lines.

index.js
  • $(function () {
  • $("#html-editor").dxHtmlEditor({
  • // ...
  • toolbar: {
  • items: [
  • "undo",
  • "redo",
  • "separator",
  • {
  • name: "size",
  • acceptedValues: [ "8pt", "10pt", "12pt", "14pt", "18pt", "24pt", "36pt" ]
  • },
  • {
  • name: "font",
  • acceptedValues: [ "Arial", "Georgia", "Tahoma", "Times New Roman", "Verdana" ]
  • },
  • "separator",
  • "bold",
  • "italic",
  • "strike",
  • "underline",
  • "separator",
  • "alignLeft",
  • "alignCenter",
  • "alignRight",
  • "alignJustify",
  • "separator",
  • "orderedList",
  • "bulletList",
  • "separator",
  • {
  • name: "header",
  • acceptedValues: [false, 1, 2, 3, 4, 5]
  • },
  • "separator",
  • "color",
  • "background",
  • "separator",
  • "link",
  • "image",
  • "separator",
  • "clear",
  • "codeBlock",
  • "blockquote",
  • "separator",
  • ],
  • multiline: true
  • },
  • });
  • });

Work with Tables

Users can insert and modify tables if you add the following items to the toolbar:

index.js
  • $(function () {
  • $("#html-editor").dxHtmlEditor({
  • // ...
  • toolbar: {
  • items: [
  • // ...
  • "insertTable",
  • "deleteTable",
  • "insertRowAbove",
  • "insertRowBelow",
  • "deleteRow",
  • "insertColumnLeft",
  • "insertColumnRight",
  • "deleteColumn",
  • "cellProperties",
  • "tableProperties"
  • ]
  • }
  • });
  • });

Users can also modify tables with the context menu if you set the tableContextMenu.enabled property to true. Note that the context menu cannot be used to create new tables because the menu is only available within table boundaries. If you want users to create tables, add an "insertTable" item to the toolbar.

index.js
  • $(function () {
  • $("#html-editor").dxHtmlEditor({
  • // ...
  • toolbar: {
  • items: [
  • // ...
  • "insertTable"
  • ]
  • },
  • tableContextMenu: {
  • enabled: true
  • }
  • });
  • });
NOTE
  • The HtmlEditor tables are native HTML tables that use native features; therefore, the same limitations apply. For example, users cannot paste multiline text in separate cells.

  • The HtmlEditor tables do not support complex elements in cells, such as block elements, lists, nested tables, etc.

View Demo

Configure Mentions

Mentions allow users to reference others within text or conversation threads. This functionality is not used in this tutorial. For information about it, refer to the following demo: Mentions.

Resize Images

Users can resize images embedded within the content. To enable this functionality, set the mediaResizing.enabled property to true:

index.js
  • $(function () {
  • $("#html-editor").dxHtmlEditor({
  • // ...
  • mediaResizing: {
  • enabled: true
  • }
  • });
  • });

For further information on the HTML Editor component, refer to the following resources: