Home arrow HTML arrow Page 4 - Building a WYSIWYG HTML Editor Part 1/2

Building a WYSIWYG HTML Editor Part 1/2

Whether you're creating your own personal web site, a site for a medium sized business, or a site for a company intranet, the content management system (CMS) is one of the most important aspects of the overall design. In this article Mitchell shows us how to build an awesome browser based HTML editor using only HTML and JavaScript. This article is a must read!

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 398
February 27, 2002
  1. · Building a WYSIWYG HTML Editor Part 1/2
  2. · Why a WYSIWYG editor
  3. · execCommand examples
  4. · Building our HTML editor
  5. · Building our HTML editor (contd.)
  6. · Building our HTML editor (contd.)
  7. · Conclusion

print this article

Building a WYSIWYG HTML Editor Part 1/2 - Building our HTML editor
(Page 4 of 7 )

Over the next couple of pages we're going to create our own browser based WYSIWYG HTML editor. I won't look at the entire source code for the editor, but I will go into detail about the most important points. The single HTML file and required images are available as part of the support material for this article. You should download it before continuing.

Firstly, we want to make our HTML editor boths look and feels like a "normal" HTML editor, such as Front Page 2000. Using style sheets and events, we can create a set of "buttons" that mimic those found in Windows applications. Our HTML editor will contain several buttons to bold text, insert images, etc. Here's how the image that makes up the bold button looks:

<img alt="Bold" class="butClass" src="bold.gif" onMouseOver="selOn(this)" onMouseOut="selOff(this)" onMouseDown="selDown(this)" onMouseUp="selUp(this)" onClick="doBold()">

It has a class attribute of "butClass", which is defined in the <head> part of our HTML document:



border: 1px solid;

border-color: #D6D3CE;


It also responds to five events, which will be fired by the browser: onMouseOver, onMouseOut, onMouseDown, onMouseUp and onClick. The contents of each of these attributes points to a JavaScript function that is defined at the top of the file. For example, when a user moves his/her mouse over a button, the selOn function is called, passing in the image as an object ("this" represents the control for which the event has been raised):

function selOn(ctrl)


ctrl.style.borderColor = '#000000';

ctrl.style.backgroundColor = '#B5BED6';

ctrl.style.cursor = 'hand';


The selOn function sets the border color of the image to black, its background color to "FrontPage blue", and the mouse cursor to a hand.

Each button in our HTML editor responds to the same five events in exactly the same way. The only difference is that when the onClick event is fired (the user clicks on a button), then each button will call a different JavaScript function, thus performing a different task.

Here's how the buttons will look at different stages:

The bold button at different stages

Besides buttons, our HTML editor also contains an inline frame (<iframe>) and drop down lists to set the font type and size, as well as various heading sizes (<h1>, <h2>, etc). If you've downloaded the support material, you will see that our HTML editor looks like this in IE:

Our HTML editor in IE

Itís amazing what you can do with a little bit of CSS and some positioning isn't it? When each button is clicked, its onClick event is fired. Here's the code for the doBold() JavaScript function, which is called when the bold "button" is clicked on:

function doBold()


iView.document.execCommand('bold', false, null);


As you can see, we've used the same execCommand that we used earlier to test bolding some text. In fact, most of our JavaScript functions only contain one line, which is the call to the inline frame's execCommand method.

The code for the italic and underline buttons is fairly similar to the bold buttons code, so let's look at the code for the center align function, doCenter():

function doCenter()


iView.document.execCommand('justifycenter', false, null);


One again, a simple call to execCommand, passing in the command "justifycenter". To actually test the justifycenter command, click on the inline frame, type in some text, select it, and click the center align button, like this:

Centring some text with the justifycenter command

The insertorderedlist and insertunorderedlist commands can be passed to execCommand to toggle whether or not the selected text is part of either an ordered or unordered list respectively. When the user clicks on the bulleted (unsorted) list button, the doBulList() JavaScript function is called. It looks like this:

function doBulList()


iView.document.execCommand('insertunorderedlist', false, null);


Here's a screen shot from me playing around with an unordered list. I've also changed the colors of the foreground and background, which we will look at next:

Playing around with an unordered list
blog comments powered by Disqus

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials