This example demonstrates how to toggle from a plain text field to the Rich Text Editor with a simple button click.
Setting up the Editor's HTML is done by creating a
textarea control on the page.
textarea is on the page, then initialize the Editor like this:
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.
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
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
Using YAHOO.util.Dom we will set
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
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
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.