function ShowCommentPreview()
{
// Access the HTML elements we care about from our page
var enteredCommentName = document.getElementById("CommentName");
var previewCommentName = document.getElementById("CommentPreviewName");
var enteredComment = document.getElementById("CommentBody");
var previewComment = document.getElementById("CommentPreviewBody");
// Let javascript do it's final bit of magic
previewCommentName.innerHTML = enteredCommentName.value + " Says: ";
previewComment.innerHTML = enteredComment.value;
}
As you can see the javascript is almost self explanatory. First, I declared variables and i assigned various id's of the HTML elements i cared about to them from our page. Finally i take the value of the entered comment and placed it as the value of the comment preview.
<textarea id="CommentBody" onkeydown="ShowCommentPreview();"></textarea>
Finally, i added a HTML attribute to the textarea called the "onKeydown" which is a special javascript thing that we use to wired up the HTML and the javascript method to do the final trick.
That's it ... pretty easy thing to do. I hope you will enjoy building it on your own website. Good luck.
Download the Sample file live-comment preview.zip (4kb).