<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Xylot Design &#187; rich text</title>
	<atom:link href="http://xylotdesign.wordpress.com/tag/rich-text/feed/" rel="self" type="application/rss+xml" />
	<link>http://xylotdesign.wordpress.com</link>
	<description>useful scripts, ideas, and tips!</description>
	<lastBuildDate>Fri, 21 Mar 2008 20:39:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='xylotdesign.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d47e221117c84d128bbe00e39a6140a7?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Xylot Design &#187; rich text</title>
		<link>http://xylotdesign.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://xylotdesign.wordpress.com/osd.xml" title="Xylot Design" />
		<item>
		<title>JS: WYSISYG Rich Text Box</title>
		<link>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/</link>
		<comments>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 01:25:57 +0000</pubDate>
		<dc:creator>Xylot Design</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[rich text]]></category>
		<category><![CDATA[size]]></category>
		<category><![CDATA[textarea]]></category>
		<category><![CDATA[word processing]]></category>
		<category><![CDATA[wysiwyg]]></category>

		<guid isPermaLink="false">http://xylotdesign.wordpress.com/?p=9</guid>
		<description><![CDATA[Many sites are starting to add rich text boxes, which are text fields that let you create text and alter formatting in a WYSIWYG environment: &#8220;What You See Is What You Get&#8221;. This basically means a text box that is like a word processor; if you select text and click on the &#8220;Bold&#8221; button (such [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=9&subd=xylotdesign&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Many sites are starting to add rich text boxes, which are text fields that let you create text and alter formatting in a WYSIWYG environment: &#8220;What You See Is What You Get&#8221;. This basically means a text box that is like a word processor; if you select text and click on the &#8220;Bold&#8221; button (such as a bold letter <b>B</b>), the text will actually show up in bold.</p>
<p>If such a concept has eluded you, look no further for a detailed tutorial on how to create one.<span id="more-9"></span></p>
<p>If you have been thinking of a rich text field as its own control (like the <b>&lt;textarea&gt;</b> element), then you are thinking about it all wrong. Instead, think of the text field as being its own web page.  It makes sense, doesn&#8217;t it? You&#8217;re applying HTML formatting to the text, after all.</p>
<h1>The Structure</h1>
<p>The text field itself is an <b>&lt;iframe&gt;</b> element, which (quite conveniently) comes with a JavaScript function called <b>execCommand()</b>. This function will become your colleague and your confidant. Your most trusted ally, and your most intimate friend.</p>
<p>To start off, create a new page with an iframe, a regular button, a submit button, and a hidden text field. The regular button will toggle the <b>bold</b> setting of any selected text within the iframe. The submit button is there for decoration only, and as a reminder that sometimes a form will need to be submitted. As such, the iframe cannot be used as a form input field; instead, we will use the hidden text field to store the HTML code of the iframe element. Its contents will be updated upon submitting the form, as we will see later.</p>
<pre>&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Rich Text Editor&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method="post" name="myform"&gt;
	&lt;input type="button" onclick="formatText('bold')" value="Bold" /&gt;&lt;br/&gt;
	&lt;iframe id="textbox" style="width: 300px; height: 150px"&gt;&lt;/iframe&gt;&lt;br/&gt;
	&lt;input type="submit" value="Submit" /&gt;
	&lt;input type="hidden" id="text" name="text" /&gt;
&lt;/form&gt;
&lt;/body&gt;
/html&gt;</pre>
<h1>The Backbone</h1>
<p>The <b>formatText()</b> function, called by clicking the &#8220;Bold&#8221; button, is defined below. You can either save it in an external <b>.js</b> file and call it from the HTML page, or add it to the <b>&lt;head&gt;</b> section.</p>
<pre>var objEditor;	// Globally define the text box object
unction formatText(action) {
	objEditor.execCommand(action, false, null);
}
window.onload = function() {
	objEditor = document.getElementById('textbox').contentWindow.document;
	objEditor.designMode = 'on';
	document.myform.onsubmit = function() {
		document.getElementById('text').value = objEditor.body.innerHTML;
	}
}</pre>
<p>As you can see above, the text editor object (with the ID &#8220;textbox&#8221;) is set to a global variable, which can then be called by any other functions. The <b>formatText()</b> function calls the omnipotent <b>execCommand()</b> function, which applies only to the <b>&lt;iframe&gt;</b> object and is extremely powerful. There is a complete list of commands below. The command used above is the &#8220;bold&#8221; command. The second argument determines whether a user interface is displayed. Since this is not needed, it is set to false. The third argument is the value of the command. Since we are toggling the &#8220;bold&#8221; setting, we set this to <b>null</b>.</p>
<p>Next, we define what will happen when the page loads by setting the document&#8217;s <b>onload</b> event handler to do a few things. First, the text editor object variable is set to the object itself. Next, the text editor iframe&#8217;s <b>designMode</b> setting is turned on; this makes it so that the HTML within the iframe is editable. This is a huge deal, because this is essentially what turns the iframe into a rich text field.</p>
<p>(Actually, you can even set the entire document&#8217;s <b>designMode</b> variable to &#8220;on&#8221;, rendering the entire document editable. Just imagine the possibilities!)</p>
<p>Finally, we set it so that when the user submits the form, it sets the &#8220;text&#8221; hidden field&#8217;s value to the HTML of the iframe.</p>
<h1>The Commands</h1>
<p>The following commands are some of the simpler commands that can be used: <b>Bold</b> (&lt;b&gt;&lt;/b&gt;), <b>Italic</b> (&lt;i&gt;&lt;/i&gt;), <b>Underline</b> (&lt;u&gt;&lt;/u&gt;), <b>StrikeThrough </b>(&lt;s&gt;&lt;/s&gt;), <b>SuperScript </b>(&lt;sup&gt;&lt;/sup&gt;), <b>SubScript </b>(&lt;sub&gt;&lt;/sub&gt;), <b>Cut</b>, <b>Copy</b>, <b>Paste</b>, <b>Undo</b>, <b>Redo</b>, <b>InsertOrderedList</b> (&lt;ol&gt;&lt;/ol&gt;), <b>InsertUnorderedList</b> (&lt;ul&gt;&lt;/ul&gt;), <b>Outdent</b>, <b>Indent</b>, <b>JustifyLeft</b>, <b>JustifyCenter</b>, <b>JustifyRight</b>, and <b>JustifyFull</b>.</p>
<p>SuperScript and SubScript raise or lower text, for things such as mathematic variables (like 7<sup>2</sup> and H<sub>2</sub>O). The commands Cut, Copy, Paste, Undo and Redo function the same way they do in word processors. InsertOrderedList inserts a numbered (ordered) list, while InsertUnorderedList inserts a bulleted (unordered) list. Outdent moves a paragraph of text to the left, while Indent moves it inward (to the right). The Justify commands align the text to the direction specified.</p>
<h2>Change Font Size</h2>
<p>The <b>FontSize</b> command changes the size of the selected text in the iframe, and requires a value from 1 to 7, 1 being the smallest size, 7 being quite large. This command is based on the soon-to-be obsolete <b>&lt;font&gt;</b> tag, which uses the same values for its <b>size</b> attribute.</p>
<p>This function is best used with a drop-down list whose <b>onchange</b> event handler calls a custom function that changes the font size to the selected value. To most accurately show the user what the end result would be, each line should resemble the following.</p>
<pre>&lt;option value="X"&gt;&lt;font size="X"&gt;X&lt;/font&gt;&lt;/option&gt;</pre>
<p>Each item&#8217;s value is the size number, while the item is shown in that size, displaying the size number.</p>
<h1>Change Font Name</h1>
<p>There is a certain list of fonts that is safe to assume all users have installed on their systems. Known as the <b>Core Fonts</b>, these include Andale Mono, Arial, Comic Sans MS, Courier New, Georgia, Impact, Times New Roman, Trebuchet MS, Verdana, and Webdings. It is also safe to include a few fonts that are on most users&#8217; systems, but not all: Arial Black, Lucida Console, Lucida Sans Unicode, Tahoma, and Wingdings.</p>
<p>Like we did with the font sizes, if we had a drop-down list of fonts, each displayed in its own font, we could then set the list&#8217;s <b>onchange</b> event handler to call up the <b>FontName</b> command, like so:</p>
<pre>objEditor.execCommand("FontName", false, "Times New Roman");</pre>
<p>It&#8217;s pretty straightforward how it works.</p>
<h1>Further Inspiration</h1>
<p>There are so many possibilities, especially with the sheer number of commands available at your disposal. For example, there is the <b>2D-Position</b> command, which allows absolutely positioned elements to be moved by dragging. There is also the <b>CreateLink</b>, which adds a hyperlink or changes text into a hyperlink. <b>InsertButton</b>, <b>InsertImage</b> and<br />
<b>SaveAs</b> are just a microscopically small selection of commands also at your disposal. For a full list of commands available, check out <a href="http://msdn2.microsoft.com/en-us/library/ms533049(VS.85).aspx" target="_blank">http://msdn2.microsoft.com/en-us/library/ms533049(VS.85).aspx</a>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/xylotdesign.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/xylotdesign.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/xylotdesign.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/xylotdesign.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/xylotdesign.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=xylotdesign.wordpress.com&blog=3153931&post=9&subd=xylotdesign&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://xylotdesign.wordpress.com/2008/03/17/js-wysisyg-rich-text-box/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b31bda9bb80f4c9b9f507a02539c79e7?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Xylot Design</media:title>
		</media:content>
	</item>
	</channel>
</rss>