A lightweight, zero-dependency WYSIWYG editor that's easy to implement and supports multiple instances on the same page.
Everything you need in a lightweight package
Only ~7KB total (JS + CSS). No jQuery, no frameworks, just vanilla JavaScript ES6.
Works out of the box. Only requires Font Awesome for toolbar icons (optional).
Create unlimited editor instances on the same page with independent settings.
Dynamically resize editors via API. Images inside the editor are also drag-resizable.
Toggle between WYSIWYG and raw HTML source view with a single click.
Syncs automatically with hidden textarea for seamless form submission.
Try out multiple editors on the same page
Get up and running in under 2 minutes
Download editor.js and editor.css to your project folder.
your-project/
âââ editor.js (5.5 KB)
âââ editor.css (2.1 KB)
âââ index.html
Add Font Awesome (for icons) and the editor files to your HTML.
<!-- In your <head> -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="editor.css">
<!-- Before closing </body> -->
<script src="editor.js"></script>
Add a div with data-editor attribute and a hidden textarea for form submission.
<form action="your-handler.php" method="post">
<!-- Editor container -->
<div class="editor" data-editor="myEditor"></div>
<!-- Hidden textarea for form submission -->
<textarea name="myEditor" style="display:none;"></textarea>
<button type="submit">Submit</button>
</form>
Initialize all editors with one line of code.
document.addEventListener("DOMContentLoaded", () => {
// Initialize all editors on the page
WYSIWYGEditor.initAll(".editor");
});
Creating multiple independent editors on the same page
<!-- First Editor -->
<div class="editor" data-editor="editor1"></div>
<textarea name="editor1" style="display:none;"></textarea>
<!-- Second Editor -->
<div class="editor" data-editor="editor2"></div>
<textarea name="editor2" style="display:none;"></textarea>
<!-- Third Editor with custom size -->
<div class="editor" data-editor="editor3" style="width:400px;height:300px;"></div>
<textarea name="editor3" style="display:none;"></textarea>
<script>
document.addEventListener("DOMContentLoaded", () => {
// Initialize all editors at once
WYSIWYGEditor.initAll(".editor");
// Access individual editors for customization
const editor1 = document.querySelector('[data-editor="editor1"]').__wysiwygEditor__;
editor1.updateSize(600, 400);
const editor2 = document.querySelector('[data-editor="editor2"]').__wysiwygEditor__;
editor2.updateSize(500, 300);
});
</script>
Methods and properties
| Method | Description |
|---|---|
WYSIWYGEditor.initAll(selector) |
Initialize all elements matching the CSS selector as editors |
| Method | Description |
|---|---|
updateSize(width, height) |
Dynamically update the editor dimensions |
insertLink() |
Prompt user to insert a hyperlink |
insertImage() |
Prompt user to insert an image by URL |
toggleSource() |
Toggle between WYSIWYG and source HTML view |
// Get editor instance by data-editor attribute
const editor = document.querySelector('[data-editor="myEditor"]').__wysiwygEditor__;
// Use instance methods
editor.updateSize(800, 500);
editor.toggleSource();
Built-in formatting options
| Icon | Action | Description |
|---|---|---|
| Bold | Make selected text bold | |
| Italic | Make selected text italic | |
| Underline | Underline selected text | |
| Align Left | Align text to the left | |
| Align Center | Center align text | |
| Align Right | Align text to the right | |
| Unordered List | Create a bulleted list | |
| Ordered List | Create a numbered list | |
| Insert Link | Add a hyperlink to selected text | |
| Insert Image | Insert an image by URL | |
| Source View | Toggle HTML source code view |
Get the files and start building
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/stephantheron/steditor@latest/editor.css">
<script src="https://cdn.jsdelivr.net/gh/stephantheron/steditor@latest/editor.js"></script>
MIT License - Free for personal and commercial use
Copyright ÂĐ 2024 Stephan Theron
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial
portions of the Software.