YUI Library Home

YUI Library Examples: Rich Text Editor: Plain Text Switcher

Rich Text Editor: Plain Text Switcher

This example demonstrates how to toggle from a plain text field to the Rich Text Editor with a simple button click.


Setting up the Editors HTML

Setting up the Editor's HTML is done by creating a textarea control on the page.

Setting up the Editors Javascript

Once the textarea is on the page, then initialize the Editor like this:

Now handle the Toggle

Now we will create a Button control and attach a click event to it.

From the click event we will determine the state of the editor (either "on" or "off"). Then we will choose to either hide or show it,

Note: It is not recommended to set the editor to display: none. This causes certain browsers to lose the editor, you should use visibility: hidden or top, left (to move it out of the viewable area).

Before showing or hiding the Editor, we need to clean up the HTML we are using.

Switching from the Editor to the textarea, we need to strip the HTML from our output and replace all <br>'s with line breaks. This code snippet will handle this for our example. Your implementation may need a stronger approach.

Note: You "could" add a button to the toolbar and have it execute the toggle. Then remove these few lines from the example and you will have a source editor.

Going from the Editor to the textarea

First we must call myEditor.saveHTML(). This method will clean up the HTML in the Editor and return it to the textarea.

Once it is in the textarea, we will process it to remove all of the HTML and replace the <br>'s with line breaks.

Now using YAHOO.util.Dom we will set top, left and position: absolute (this will keep the Editor from taking up page space) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to hidden.

Going from the textarea to the Editor

Using YAHOO.util.Dom we will set top: 0, left: 0 and position: static (to put the Editor back in the page) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to visible.

Then we call the Editor method _setDesignMode('on') to re-enable designMode since it may have been lost with the visibility change.

Now we call the Editor method setEditorHTML() passing it the value of the textarea with the line breaks all converted back to <br>'s.

Full Example Javascript Source

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings