• Sign In
Logo

Create your own website in seconds with easy to use
site design tools and have your content appear here.

  Visit http://geographicalmedia.org to build your own custom site! 

  • Home
  • News
  • Posts
  • Photos
  • Talk
  • Real Estate
  • Communities
« How to Record the computer screen withou...
iPod Shuffle - Music on the move! »

How to build a live comment preview with Javascript and HTML

Edit Display Settings

or
Edit Post
africa » gambia
Thursday, March 27, 2008
In recent times, it has become quite a trendy and nifty trick to show a live comment preview on a lot of blog websites. I remember just how excited i was the first time i saw this in action on a dotnet blogengine application. In this article i will show you how to implement a live comment preview on your website with just simple javascript and HTML.

Okay, to get started i will start by creating a javascript(.js) file with the following on it.

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.

Some of the HTML

<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).
 

Comments

 
There are 3 comments - add your comment
Ali - England, United Kingdom
Tuesday, April 08, 2008 12:59 PM
seems nice, i might try this out later
as - Bani, Burkina Faso
Wednesday, June 25, 2008 9:57 PM
whaaat
Sava - Braila, Romania
Saturday, June 28, 2008 3:29 AM
Pretty nice. I like it.

P.S. I also like the location thingy :D

Your Comment

Your Name
Your Location
To receive emails create a username or Sign In
Your Email
Username
Password
Confirm Password
Add me to the contact list.
Remember me on this computer.
Sign in below or Sign Up
Username
Password
loading...  
 
Related Topics
javascript, web
Provided By
Media Actions
2
Promote
Email to a friend
Enquire
Save to delicious
Digg this
Stumble it
See Also

    .geographical media

    Visit http://geographicalmedia.org to build your own website!

    Site created with .geographical media. Explore geo