YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>

## Setting up the Editors HTML

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

<button type="button" id="toggleEditor">Toggle Editor</button> <form method="post" action="#" id="form1"> <textarea id="editor" name="editor" rows="20" cols="75"> This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> This is some more test text.<br>This is some more test text.<br>This is some more test text. </textarea> </form>

## Setting up the Editors Javascript

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

(function() { //Setup some private variables var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event; //The Editor config var myConfig = { height: '300px', width: '600px', animate: true, dompath: true, focusAtStart: true }; //Now let's load the Editor.. var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); })();

## 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.

//From the Editor to the textarea var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); //From the textarea to the Editor myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>'));

## 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`.

myEditor.saveHTML(); var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); var fc = myEditor.get('element').previousSibling, el = myEditor.get('element'); Dom.setStyle(fc, 'position', 'absolute'); Dom.setStyle(fc, 'top', '-9999px'); Dom.setStyle(fc, 'left', '-9999px'); myEditor.get('element_cont').removeClass('yui-editor-container'); Dom.setStyle(el, 'visibility', 'visible'); Dom.setStyle(el, 'top', ''); Dom.setStyle(el, 'left', ''); Dom.setStyle(el, 'position', 'static');

## 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.

var fc = myEditor.get('element').previousSibling, el = myEditor.get('element'); Dom.setStyle(fc, 'position', 'static'); Dom.setStyle(fc, 'top', '0'); Dom.setStyle(fc, 'left', '0'); Dom.setStyle(el, 'visibility', 'hidden'); Dom.setStyle(el, 'top', '-9999px'); Dom.setStyle(el, 'left', '-9999px'); Dom.setStyle(el, 'position', 'absolute'); myEditor.get('element_cont').addClass('yui-editor-container'); YAHOO.log('Reset designMode on the Editor', 'info', 'example'); myEditor._setDesignMode('on'); YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example'); myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>'));

## Full Example Javascript Source

(function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event; YAHOO.log('Create Button Control (#toggleEditor)', 'info', 'example'); var _button = new YAHOO.widget.Button('toggleEditor'); _button.addClass('toggleEditor'); var myConfig = { height: '300px', width: '600px', animate: true, dompath: true, focusAtStart: true }; var state = 'on'; YAHOO.log('Set state to on..', 'info', 'example'); YAHOO.log('Create the Editor..', 'info', 'example'); var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); _button.on('click', function(ev) { Event.stopEvent(ev); if (state == 'on') { YAHOO.log('state is on, so turn off', 'info', 'example'); state = 'off'; myEditor.saveHTML(); YAHOO.log('Save the Editors HTML', 'info', 'example'); var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); YAHOO.log('Strip the HTML markup from the string.', 'info', 'example'); YAHOO.log('Set Editor container to position: absolute, top: -9999px, left: -9999px. Set textarea visible', 'info', 'example'); var fc = myEditor.get('element').previousSibling, el = myEditor.get('element'); Dom.setStyle(fc, 'position', 'absolute'); Dom.setStyle(fc, 'top', '-9999px'); Dom.setStyle(fc, 'left', '-9999px'); myEditor.get('element_cont').removeClass('yui-editor-container'); Dom.setStyle(el, 'visibility', 'visible'); Dom.setStyle(el, 'top', ''); Dom.setStyle(el, 'left', ''); Dom.setStyle(el, 'position', 'static'); } else { YAHOO.log('state is off, so turn on', 'info', 'example'); state = 'on'; YAHOO.log('Set Editor container to position: static, top: 0, left: 0. Set textarea to hidden', 'info', 'example'); var fc = myEditor.get('element').previousSibling, el = myEditor.get('element'); Dom.setStyle(fc, 'position', 'static'); Dom.setStyle(fc, 'top', '0'); Dom.setStyle(fc, 'left', '0'); Dom.setStyle(el, 'visibility', 'hidden'); Dom.setStyle(el, 'top', '-9999px'); Dom.setStyle(el, 'left', '-9999px'); Dom.setStyle(el, 'position', 'absolute'); myEditor.get('element_cont').addClass('yui-editor-container'); YAHOO.log('Reset designMode on the Editor', 'info', 'example'); myEditor._setDesignMode('on'); YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example'); myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); } }); })();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Rich Text Editor Examples:

- Flickr Image Search
- Calendar Plugin
- Icon Insertion
- Plain Text Switcher
- Editor Data Post with Connection Manager
- Skinning the Editor
- Default Toolbar Config
- Code Editor
- Editor inside a Tabview Control
- One Editor, Multiple Edit Areas
- Editor with Custom Image Browser
- Simple Editor — Basic Buttons
- Simple Editor — Advanced Buttons
- Editor — Basic Buttons
- Editor Auto Adjusting Height
- Editor in a Dialog Control
- Custom resizing for the Rich Text Editor (included with examples for Resize Utility)
- Simple Application (included with examples for Layout Manager)
- Complex Application (included with examples for Layout Manager)

### More Rich Text Editor Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

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