Add a rich text area in your html form
It's easy for us lazy developers to just throw in a textarea to gather chunks of input from a user. But the problem with a textarea is that it sends plain text to your web app. You lose any kind of formatting if your user copies and pastes html into a textarea. That's no good. A good paste, though, will preserve formatting and structure so you can process it on the server.
But good news! Making a rich text area is easier than you'd think. So the thing to do is to allow your users to copy and paste into a rich text area if you want to process formatted input on your server. Here's how.
1. Create an iframe inside your form. Forget about textarea; we want to make an iframe that users can paste into. And while we're here, just add an empty hidden input field as well (more on this hidden input field in step 3).
<form id="form" action="/process"> <iframe id="paste"></iframe> <input type="hidden" id="data" /> <input type="submit" /> </form>
2. Write some javascript. An iframe is not editable in its default state. To make the iframe editable, you need to set the iframe's content document's designMode="on" after the iframe loads. I'm using prototype for this chunk of javascript:
<script type="text/javascript">
document.observe('dom:loaded', function() {
// make the iframe editable. ff and safari use contentDocument;
// ie7 uses contentWindow.document
var doc = $('paste').contentDocument || $('paste').contentWindow.document;
doc.designMode = 'on';
}
</script>
3. Almost done. You need just a bit more javascript. Since an iframe isn't an input field, its contents won't be submitted as part of the form. So you need to do a little bit of javascript trickery to populate the hidden field you added in step 1 with the iframe's content. Here's the full javascript, which includes the bits from step 2.
<script type="text/javascript">
document.observe('dom:loaded', function() {
// make the iframe editable. ff and safari use contentDocument;
// ie7 uses contentWindow.document
var doc = $('paste').contentDocument || $('paste').contentWindow.document;
doc.designMode = 'on';
// populate the hidden field with the iframe's contents to submit
// it with the form
$('form').observe('submit', function() {
var html = doc.body.innerHTML;
$('data').value = html;
});
}
</script>
4. And, remarkably, that's it. You're done. Now when a user submits the form, javascript will populate the hidden field with the iframe's contents and then send that to the server's /process action.
One (big) caveat, though. Different browsers and different platforms submit different styles of html. It's easy to get into the undesirable mode of writing browser/platform dependent code for your web app if you misuse this iframe thing.
Hey I tried this code. I am not able to make the iFrame editable....
did all whats mentioned here, but with no results.
Posted by: Sujit | October 22, 2008 at 11:42 AM
It's working!! just a small piece is missing from the javascript code. An ending bracket with the semicolon. That's it....it's working fine now!
huned it would be nice if you can show us how to create an WYSIWYG editor which can have colors and images also!!
Posted by: Sujit | October 22, 2008 at 11:54 AM
hi,
i used this code by adding the brackets and semicolon at the last in javascript but it didn't work ? y
Posted by: Krishan Kumar | October 30, 2008 at 05:02 AM
Remember linking the prototype script somwhere in your code!
:D
Posted by: Jørund Ruud | November 07, 2008 at 08:18 AM
asdasdas
Posted by: asdasd | April 29, 2009 at 09:50 AM