How to build your own WYSIWYG Editor

Sai gowtham
codeburst
Published in
1 min readMay 4, 2018

--

Have you seen WYSIWYG editors Today I’m showing the simple version of WYSIWYG editor.

If you’re familiar with HTML and JavaScript then you’re good to go.

So let’s start our simple version
I’m not explaining everything but Important things

first, we need to add a contentEditable attribute to our HTML element

<div class="editor" contenteditable>
<h1>Simple Html editor</h1>
<p>Good to start</p>
</div>

Now, div element is editable and our HTML document exposes execCommand.

Let’s see what is execCommand

document.execCommand(cmd,defaultUI,value);

execCommand needs three arguments

cmd : It means we have to tell which command needs to execute.
example:’bold’ commandslist

defaultUI :it is boolean whether default user interface is shown or not.

value : We need to add value argument to some commands.
example: In createLink cmd, we need to add a value it refers to href attribute in the link.

So lets make it use of execCommand in practice

Now I’m creating button HTML element and attached an event listener to it.

Happy coding..

checkout my chrome extension Html Scan to copy text or code instantly with one click.

--

--