Velo Tutorial: Creating a Rich Text Editor Using the HTML Component

Before reading this article, you might want to learn about Working with the HTML Component in Velo.

In this article, we demonstrate how to use an HTML Component to add a rich text editor to your page. 

We start by creating the following page:

The left side of the page is a Wix Text element.

The right side of the page is created with an HTML Component. The component contains HTML that creates a CKEditor and a button. 

Using this setup, a user can enter rich text in the rich text editor, click the Submit text button, and the rich text will be displayed in the Wix text element.

Page Code

The following code is added to the code editor.

In the page's onReady() event handler, we set an event handler that runs when the page receives a message from the HTML Component. The event handler sets the text of the page's Text element to be the data sent from the HTML Component.

Copy

Note: We are using the Text element's html property to set its styled text. To learn about how a Text element displays content that is set using the html property, see Formatting Text Elements with Velo.

HTML Component Code

The following code is added to the HTML Component using the Enter Code or  Edit Code button. It contains three main sections.

In the <body> tag, we define the HTML elements that make up our page:

  • A <textarea> that is used by the CKEditor to create a rich text editor.
  • A <button> that is used for sending the contents of the rich text editor to the page code.

In the <style> tag, we define some styles that are used to restyle the button. These styles aren't necessary if you don't want to restyle the button.

In the <script> tag, we define our messaging logic. There are two parts to the messaging logic.

First, when the HTML component loads, we use the CKEditor replace() function to turn our regular HTML <textarea> into a CKEditor rich text editor.

Next, when the button in the HTML Component is clicked, it calls the sendText() function. We define the sendText() function to send the text in the rich text editor to the page code.

Copy

API List

The following API is used in the code in this article. To learn more, see the API Reference.

Was this helpful?
Yes
No