Skip to content


Live Comment Previews

The ‘Live Comment Preview’ hack that I use on The Long Letter has now been implemented here on Eclecticism. If you know what I’m talking about, then we’re good to go. If you don’t know what I’m talking about, then click on the “Comments” link to any post, type something in the comment box, and look just below the comment box. It’s a nifty trick. ;)

I originally picked this up from ScriptyGoddess, with help from Phillip. Geeky tech details on my implementation here follow.

All this is is a nice little JavaScript addition to the page. I don’t believe that this will constitute a security risk, but I’m no expert, so use at your own risk. ;) Obviously, if you do want to use this on your site, you’ll need to have access to your templates.

In the header of the individual entry template, just after the already included JavaScript bits, I added the following code:

<script type="text/javascript">
 var newline = /\n/g;

 function ReloadTextDiv() {
  var NewText = document.getElementById("text").value;
  NewText = NewText.replace(newline, "<br />");
  var DivElement = document.getElementById("TextDisplay");
  DivElement.innerHTML = NewText;
  }
</script>

Then, in the body of the template, just after the closing </div> tag following the preview and submit buttons, but before the </form> tag, I added the following:

<br />
<h2>Live Comment Preview:</h2>
<p><span id="TextDisplay">Note: if you're comfortable with HTML, feel free to use it in your comments. If not, just type away. Single returns will be automatically converted into linebreaks (&lt;br /&gt;), double returns will be converted into paragraph breaks (&lt;p&gt;). This text will disappear as soon as you start typing.</span></p>

Lastly, in the textarea tag that defines where the comment text is entered in by a visitor, I added a onkeyup="ReloadTextDiv();" declaration. The full textarea tag should look as follows:

<textarea tabindex="4" id="text" name="text" rows="10" style="width: 80%;" onkeyup="ReloadTextDiv();">

What all this does is actually simple enough. As a visitor enters their comment into the comment box, each time they release a key the onkeyup function calls the ReloadTextDiv JavaScript snippet that I added. This function loads any text inside the comment box (identified by its ID of "text"), replaces any carriage returns with <br /> tags so that line breaks appear correctly, then writes the output into the element identified as "TextDisplay" — in this case, between the span tags I added after the submit and preview buttons.

If you find this useful, feel free to use it in your own pages. While I wouldn’t refuse credit, it really does belong to ScriptyGoddess and Phillip. Enjoy!

Posted in TypePad, Website. Tagged with , , , , , . See also: RSS Templates for TypePad Pro/MovableType |Issue with my comments-only feed |Destinations |Importing MT archives: month by month |Help search engines index your site .

17 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. This is just a quick test comment to see if rebuilding will break the page.

  2. I love this, it’s so wonderful. It gives the whole commenting system a different feel, and I think overall, it makes it more interesting to comment for readers. Good going!

  3. kris said

    test

  4. max said

    just a test

  5. bozo said

    just testing this thing

  6. dave said

    hey

  7. test tester

  8. jj said

    hello

  9. sveta said

    zetia ¶

  10. West said

    What is the deal? Are people test commenting on an ancient blog?

  11. Mike said

    sure why not

Continuing the Discussion

  1. American Swami News linked to this post on July 21, 2003

    Live Comment Preview

    Thanks to Michael Hanscom over at Eclecticism for this wonderful idea of being able to preview what you type in the comments, all live and on the fly. If you have a Typepad blog, take a look: Live Comment Preview

  2. Joel Blain dot Com linked to this post on July 31, 2003

    Live Comment Preview

    Thanks to Michael (thanks for all the help) I was able to add Live Comment Previewing! It’s a pretty cool feature that lets vistors watch their comment appear on-site as they type. Leave a comment to test it out.

  3. Snowblink linked to this post on February 12, 2004

    Live Comments

    Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…

  4. Snowblink linked to this post on February 12, 2004

    Live Comments

    Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…

  5. Snowblink linked to this post on February 12, 2004

    Live Comments

    Thanks to Michael, I have implemented Live Comments. They allow you to see your comments as you type them. Please note that although you may see your HTML in the live comments, it will be stripped. Best to preview your…

  6. made in costa rica linked to this post on December 26, 2004

    Tweaks

    So far, this has been a very productive Christmas Day. I’ve been tweaking the site, fixing small errors/problems mainly, here’s what i did: In the main page, i’ve added a link to the Category where the post was made in,…

HTML stripped, use Markdown [?] for formatting.

(required)

(required, but never shared)

or, reply to this post via trackback.

Note: This post is over 5 years old. You may want to check later in this blog to see if there is new information relevant to your comment.