Jquery HTML Javascript

how to create a code editor for your website with ace editor


In this article, we will explore how to create a simple HTML code editor using the Ace Editor library and Bootstrap. The Ace Editor is a powerful code editor that provides syntax highlighting and other useful features for editing code in various programming languages. By the end of this tutorial, you will have a functional HTML code editor that allows you to write and preview HTML code in real-time.

- Basic knowledge of HTML, CSS, and JavaScript.
- Familiarity with Bootstrap and its components.

Step 1: Setting up the HTML Structure
We will start by creating the basic HTML structure for our code editor. Copy and paste the following code into your HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
   <!-- This is the head section -->
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
   <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
   <style>
       .ace_editor {
           height: 300px;
       }
   </style>
</head>
<body>
   <div class="container">
       <div class="py-5 text-center">
           <h1>Ace Editor</h1>
           <div class="form-group">
               <!-- This is the editor div -->
               <p id="editor" class="mt-4"><h4>GET IN TOUCH</h4> 
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam euismod leo nec dui dapibus, congue fringilla sem malesuada. Maecenas sit amet massa viverra diam ultricies dignissim. Fusce fermentum, eros vel gravida commodo.</p></p>
               <!-- This is the editor textarea -->
               <textarea class="form-control mt-4" id="editor_val" style="height: 238px;" name="html" cols="50" rows="10"><h4>GET IN TOUCH</h4> 
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam euismod leo nec dui dapibus, congue fringilla sem malesuada. Maecenas sit amet massa viverra diam ultricies dignissim. Fusce fermentum, eros vel gravida commodo.</p></textarea>
           </div>
       </div>
   </div>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.5/ace.js" type="text/javascript" charset="utf-8"></script>
   <script type="text/javascript">
       // This is the JavaScript code for initializing Ace Editor
       // Create a new Ace Editor instance and associate it with the 'editor' element
       var editor = ace.edit('editor');
       // Set the theme for the Ace Editor (optional, 'monokai' theme used here)
       editor.setTheme("ace/theme/monokai");
       // Set the mode for syntax highlighting (optional, 'html' mode used here)
       editor.session.setMode("ace/mode/html");
       // Synchronize the content of the Ace Editor with the textarea
       editor.session.on('change', function(delta) {
           // Update the value of the textarea with the current content of the Ace Editor
           $('#editor_val').val(editor.getValue());
       });
   </script>
</body>
</html>


Step 2: Understanding the Code
Let's go through the key components of the code:

1. Head Section:
- We set the document's charset and viewport for responsive design.
- We include the Bootstrap CSS and JS files to style the editor and make it responsive.

2. Body Section:
- The editor is placed inside a container with the class "container."
- The "py-5" class adds some padding around the content, and "text-center" centers the text.
- The `<h1>` heading displays "HTML Code Editor" at the top of the page.

3. Ace Editor Setup:
- The `<p>` element with the ID "editor" acts as a placeholder for the Ace Editor.
- The `<textarea>` element with the ID "editor_val" holds the raw HTML code and is used for submission purposes.
- We set the height of the Ace Editor to 300px using the "ace_editor" class defined in the style section.

4. JavaScript:
- We use the Ace Editor library to create an editor instance inside the "editor" element.
- The editor's theme is set to "monokai" for a visually appealing appearance.
- The mode is set to "html" to enable syntax highlighting for HTML code.
- The editor session's "change" event is listened to, and whenever the content changes, the raw HTML is updated in the textarea with the ID "editor_val."

 

Example:


 

Document

Ace Editor

<h4>GET IN TOUCH</h4> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam euismod leo nec dui dapibus, congue fringilla sem malesuada. Maecenas sit amet massa viverra diam ultricies dignissim. Fusce fermentum, eros vel gravida commodo.</p>

Leave A Comment