YUI Library Home

YUI Library Examples: Rich Text Editor: One Editor, Multiple Edit Areas

Rich Text Editor: One Editor, Multiple Edit Areas

This example demonstrates how to have one Editor instance control different editable areas on a page.

Setting up the HTML

First we place a textarea on the page and set it to visibility hidden, then create the editable areas and assign them a class of editable

By using the .yui-editor-container class, we are setting the Editor to an absolute position of -9999px top and -9999px left to render it off of the screen.

Prep the Editor

First we are going to setup the editor with a smaller toolbar.

Showing the Editor

Now that we have the Editor rendered and positioned off of the screen, we need to be able to show it when you double click on an editable area.

We add a dblclick listener to the container editable_cont.

Inside of this listener, we grab the target of the event and check to see if it has the editable class.

Note: This logic may need to be refined for your use case as it relies on the user clicking the DIV with the editable class, so clicking on an element inside the div will fail. Most of the time, you would probably want the user to click on some sort of Edit button to exec this action.

Once we have the DIV, we set the Editor's HTML with the innerHTML of the DIV.

Next we position the Editor with the XY coordinates of the DIV.

Saving the Editor with the toolbar

Now we need to setup a way to save the Editor's data, we are going to override the toolbar's collapse button and use it as a save button.

First we need to override the text on the collapse button, we do that by overriding the var STR_COLLAPSE on the YAHOO.widget.Toolbar's prototype.

Now we listen for the toolbarLoaded event so we can attach our handler to the toolbarCollapsed event.

Inside of the toolbarCollapsed event, we will save the Editor's data back to the editable area. Then we will set the position of the editor back to -9999px left and -9999px top.

Note: You could also do this using a Save button in the toolbar.

Full Example Javascript Source

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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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