search
HomeWeb Front-endJS TutorialBuilding a text editor with Quill.js

Building a text editor with Quill.js

Quill is a free and open source text editor that falls into the category of WYSIWYG editors and is primarily used on the modern web we use today. It is a highly customizable text editor with many expressive APIs. Quill is very easy to use and provides a good interface that is easy to understand even for people with only markup experience.

In this tutorial, we will use multiple examples to explain how to build a text editor using Quill.js.

Although there are many rich text editors that are WYSIWYG text editors, the most widely used one is Quill, and the gap is very large. Now, let's learn how to use Quill.

Let’s create a basic text editor using Quill

The first step in working with Quill is to be able to use it in the editor of our choice, and to do this we need to place the two CDN links shown below in the

tag of our HTML code .
<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>

The first CDN link is Quill’s CSS style file, while the second CDN link is Quill’s JavaScript file. We need to add the two lines of code shown above to the

tag of our HTML code.

Our tag should look like this.

<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Quill Text Editor</title>
   <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
   <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
</head>

Now that we have added the CDN in the

tag, it is time to start working on the tag. Inside the tag, let's create a
tag with id="editor" and add some simple styles that specify the height. Please refer to the tag shown below.
<body>
   <div id="editor" style="height: 250px"></div>
</body>
In the above code, we create a
tag with the id "editor" and provide a simple style with a specified height of 250px

Now all that is left is to create a <script> tag inside of which we will create an instance of the Quill class and then pass the id of the <div> we created as the first parameter and the second A parameter is basically an object whose properties we specify in a text editor. </script>

Consider the <script>tag</script> shown below.

<script>
   var quill = new Quill('#editor', {
      theme: 'snow'
   });
</script>

The above <script> tag should be placed at the end of the <body> tag, that is, before the <body> tag is closed. </script>

index.html

The entire HTML code is shown below.

Example



<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Quill Text Editor</title>
   <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
   <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
</head>

   
<script> var quill = new Quill('#editor', { theme: 'snow' }); </script>

If you open the above HTML file in your browser, you will see a text editor output in your browser. In the text editor that you will see, we will have a large number of toolbar options at our disposal, any of which we can use in the text editor.

Let’s customize the appearance of the text editor

Now suppose we only want to provide two default toolbar options instead of all the options you get by default in a normal text editor. In this case we can use the <script> tag shown below. </script>

<script>
   let toolbarOptions = [
      ['bold', 'italic', 'underline']
   ]
   let quill = new Quill('#editor', {
      modules: {
         toolbar: toolbarOptions
      },
      theme: 'snow'
   });
</script>

In the <script> tag above, we have provided only three options, namely bold, italic and underline, in the toolbar, so only these options will be available to the text editor. </script>

index.html

Shown below is the updated index.html file.

Example



<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Quill Text Editor</title>
   <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
   <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
</head>

   
<script> var toolbarOptions = [ ['bold', 'italic', 'underline'] ] var quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); </script>

If you run the above file in a browser, you will only see three toolbar options in the text editor, namely the bold option, italic option and underline option.

Record the contents of the text editor in the console

Now suppose we want to log what we write in the text editor to the console. In order to do this, we first need to create a button in the

tag.

Consider the code snippet shown below that creates a button.

<button onclick="consoleHTMLContent()">Print in Console</button>

Now let's focus on the <script> tag, where we need to create a function that will actually log the contents of the quill text editor as well as some other toolbar options. </script>

Consider the updated <script> tag shown below. </script>

<script>
   let toolbarOptions = [
      ['bold', 'italic', 'underline'],[{
         'size': ['small', false, 'large', 'huge']
      }],[{
         'color': []
      }, {
         'background': []
      }]
   ]
   let quill = new Quill('#editor', {
      modules: {
         toolbar: toolbarOptions
      },
      theme: 'snow'
   });
   function consoleHTMLContent() {
      console.log(quill.root.innerHTML);
   }
</script>
In the above <script> tag, we have a function called consoleHTMLContent in which I print the content present in the root property of the quill object <p><b>index.html <p>The updated <b>index.html code is as follows. <h3>Example <pre class='brush:php;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> &lt;button onclick=&quot;consoleHTMLContent()&quot;&gt;Print in Console&lt;/button&gt; <script> let toolbarOptions = [ ['bold', 'italic', 'underline'],[{ 'size': ['small', false, 'large', 'huge'] }],[{ 'color': [] }, { 'background': [] }] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); function consoleHTMLContent() { console.log(quill.root.innerHTML); } </script>

If we run the above code in the browser, we will see a text editor. Once we enter some text in the editor and click the button, the root object of the quill text editor will be printed in the console. .

Output

I tried writing a simple line of code in the editor and then clicked the button and this is the output I got in the browser console.

<p>Hi There <strong>Inside HTML </strong><em>Is this italic?</em></p>

in conclusion

In this tutorial, we demonstrate how to use Quill.js to create a text editor with different toolbar options. Through multiple examples, we explain how to add or remove toolbars and how to control the root element in the Quill text editor.

The above is the detailed content of Building a text editor with Quill.js. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software