Create simple cross-browser textarea editor
This tutorial will help you create simple cross-browser textarea editor you can use on any of your HTML forms. View the sample.
The main DHTML methods we will use to create this cross-browser textarea editor are:
- execCommand.
This method executes a command on the current document, current selection, or the given range. - getElementById
Returns a reference to the first object with the specified value of the ID attribute.
Commands specify an action to take on the given object. Commands we will use are:
- Bold
Toggles the current selection between bold and nonbold. - Underline
Toggles the current selection between underlined and not underlined. - Italic
Toggles the current selection between italic and nonitalic. - JustifyCenter
Centers the format block in which the current selection is located. - JustifyLeft
Left-justifies the format block in which the current selection is located. - JustifyRight
Right-justifies the format block in which the current selection is located. - Indent
Increases the indent of the selected text by one indentation increment. - Outdent
Decreases by one increment the indentation of the format block in which the current selection is located. - Undo
Undo the step - Redo
Redo the step
Main DHTML properties we will use are:
- innerHTML
Sets or retrieves the HTML between the start and end tags of the object. - designMode
Sets or retrieves a value that indicates whether the document can be edited.
First, we will create the form.htm page containing the buttons and a javascript code that will call the functions which display and update editors textarea. I will not take too much time explaining the html code except that the form is using form.php file to display content submitted.
Download the button images here.
|
<html> </head> <td> <a href= "javascript:editorCommand('content', 'underline', '')" ><img src="images/underline.gif" width="25" height="24" alt="Underline" title="Underline"></a> </td> <td> <a href= "javascript:editorCommand('content', 'italic', '')" ><img src="images/italic.gif" width="25" height="24" alt="Italic" title="Italic"></a> </td> <td> <a href= "javascript:editorCommand('content', 'justifyleft', '')" ><img src="images/j_left.gif" width="25" height="24" alt="Align Left" title="Align Left"></a> </td> <td> <a href= "javascript:editorCommand('content', 'justifycenter', '')" ><img src="images/j_center.gif" width="25" height="24" alt="Align Center" title="Align Center"></a> </td> <td> <a href= "javascript:editorCommand('content', 'justifyright', '')" ><img src="images/j_right.gif" width="25" height="24" alt="Align Right" title="Align Right"></a> </td> <td> <a href= "javascript:editorCommand('content', 'indent', '')" ><img src="images/indent.gif" width="25" height="24" alt="Indent" title="Indent"></a> </td> <td> <a href= "javascript:editorCommand('content', 'outdent', '')" ><img src="images/outdent.gif" width="25" height="24" alt="Outdent" title="Outdent"></a> </td> <td> <a href= "javascript:editorCommand('content', 'undo', '')" ><img src="images/undo.gif" width="25" height="24" alt="Undo" title="Undo"></a> </td> <td> <a href= "javascript:editorCommand('content', 'redo', '')" ><img src="images/redo.gif" width="25" height="24" alt="Redo" title="Redo"></a> </td> <td width="100%" align="levt">Shift+Enter for single line spacing</td> initiateEditor(); </body> |
So let's create the javascript file that does all the work called editor.js.
|
//First lets initiate some variables var isEditable= false; function initiateEditor() { function displayEditor(editor, html, width, height) { //this is designer function that enables designMode and writes defalut text to the text area //To execute command we will use javascript function execCommand. function updateEditor(editor) { |
Now that we have the JavaScript code let's create form.php file. Most important part of the form.php is the phpSafe function. This function makes sure that quotes are displayed appropriatly.
|
<?php |
Emir Plicanic owns and operates a web design company, is a graphic designer at a nation wide company, and enjoys teaching Dreamweaver to enthusiastic students at a local college.