Inspired by Phil Haack of ASP.NET MVC fame, I decided to write my own live preview for the Ruby on Rails simple_format helper.
Since it’s for Rails, it’s written in Prototype, which is a departure from my usual work in jQuery. I actually worked with Prototype first, before switching to jQuery. I switched because I now work in a Microsoft shop and jQuery comes prepackaged with ASP.NET MVC.
The simple_format helper is pretty simple. It converts single newlines to break tags and double newlines to paragraphs. The live previews watches the keyup event on the textarea field and updates the contents of the preview div. The simple_format is just that: simple!
The source is hosted on GitHub. I’ve worked with Git and GitHub for a little while now, and I totally get what all of the fuss is about. I like the same things about Git that I like about RoR. You can tell that they have taken the best of many other tools and made it all easier!
<html>
<body>
<form id="my-form">
<textarea id="my-textarea"></textarea>
<div style="clear:both;"/>
<div id="my-live-preview"></div>
</form>
<script type="text/javascript" src="./path/to/prototype.js"></script>
<script type="text/javascript" src="./path/to/simple-format-preview.js"></script>
<script type="text/javascript">
new SimpleFormatPreview('my-textarea', 'my-live-preview');
</script>
</body>
</html>