Conversations for ExpressionEngine

Quote + Reply using jQuery

Getting Started

A nifty feature that's available either as a plugin or natively in most CMS's, the ability to directly quote or reply to a comment, is unfortunetly missing in ExpressionEngine. No worries, though: this can be easily added with jQuery. In this tutorial we will be looking at how to do just that.

If you would like to skip the tutorial you can view or download a barebones example.

The Html & EE Code

First, let's make our comment submission form..

<div id="reply"> 
{exp:comment:form}
 <label> 
  <span> Name</span> 
  <input type="text" name="name" value="{name}"/> 
 </label> 
 <label> 
  <span> Email</span> 
  <input type="text" name="email" value="{email}"> 
 </label> 
 <label> 
  <span> Website</span> 
  <input type="text" name="url" value="{url}"/> 
 </label> 
 <label> 
  <span> Message</span> 
  <textarea rows="6" cols="50" 
  name="comment" id="commentbox"> 
  
  </textarea> 
 </label> 
 <label class="submit"> 
  <input type="submit" name="submit" value="send"/> 
 </label> 
{/exp:comment:form}
</div>

And this is how we're going to structure our comments. Feel free to structure them as you wish, but keep in mind that you will have to tinker with the jQuery selectors we're going to use to make it fit your needs later on. Most importantly though, regardless of how you structure your comments, make sure to set the comment container's id to the comment id.

 <ol class="comments"> 
 {exp:comment:entries}
  <li id="c_{comment_id}"> 
   <span class="author"> 
    <a href="{url}">{author}</a> wrote:
   </span> 
   <p>{comment}</p> 
   <a href="#reply" class="c_reply">Reply</a> 
   <a href="#reply" class="c_quote">Quote</a> 
  </li> 
 {/exp:comment:entries}
 </ol>

Adding the Reply Button

Alright, now to put those quote & reply buttons to good use we will attach an onclick function to find the comment id and grab the author name, url, and comment and have it paste it into the comment textbox.

Let's start by creating the function for the reply button:

$(document).ready(function(){
   $("a.c_reply").click(function() {
   });
});

Then find the comment id and author name and throw it into a variable

var author = $(this).parents("li").find("span a").html();
var authlink = $(this).parents("li").attr("id");

And finally, spit the variables out into our comment textarea

var text = $("textarea#commentbox").attr("value");
$('textarea#commentbox').attr("value",text+"<a href='#"+authlink+"'>@"+author+"</a>").focus();

Our final code should look something like this:

$(document).ready(function(){  
   $("a.c_reply").click(function() {
	 var author = $(this).parents("li").find("span a").html();
	 var authlink = $(this).parents("li").attr("id");
	 var text = $("textarea#commentbox").attr("value");
	 $('textarea#commentbox').attr("value",text+"<a href='#"+authlink+"'>@"+author+"</a>").focus();
   );
});

Adding the Quote Button

Now to make the quote button work. We'll start by adding the functionailty of the reply button:

$(document).ready(function(){
   $("a.c_quote").click(function() {
	 var author = $(this).parents("li").find("span a").html();
	 var authlink = $(this).parents("li").attr("id");
	 var text = $("textarea#commentbox").attr("value");
   });
});

Next, we'll grab the actual comment and put it into a variable:

var quote = $(this).parents("li").find('p').text();

And finally, we'll grab any highlighted text and put it into a variable. We'll then replace the whole quote with only the part that is selected:

if (window.getSelection)
  var selection = window.getSelection();
else if (document.getSelection)
  var selection = document.getSelection();
else if (document.selection) {
  var selection = document.selection.createRange().text; }        
if(selection!="") quote = selection; 

Our final code should look something like this:

$("a.c_quote").click(function() {
  var author = $(this).parents("li").find("span a").html();
  var authlink = $(this).parents("li").attr("id");
  var text = $("textarea#commentbox").attr("value");
  var quote = $(this).parents("li").find('p').text();
  if (window.getSelection)
	var selection = window.getSelection();
  else if (document.getSelection)
	var selection = document.getSelection();
  else if (document.selection) {
	var selection = document.selection.createRange().text; }        
  if(selection!="") quote = selection;
  $('textarea#commentbox').attr("value",text+"<a href='#"+authlink+"'>@"+author+"</a><cite>"+quote+"</cite>").focus();
});

Putting our code together we should have something that works like this example. You can also download the example here.

  1. @Nick Kutateli
    I have allow the “cite” and “a” tags in the “safe HTML” list in system/core/core.typography.php but the anchorlink still like http://#c_1

    wrote kamwing on Sat, October 24, 2009 AT 08:39am
  2. @Marcus: Really? I’m not finding following the replies difficult at all. Just click the link and you an see the original comment. Granted, Nick has a rather unorthodox comment display going on here, so it does get a little confusing… but on “normal” comments, it reads just fine IMO.

    wrote Chris on Fri, June 12, 2009 AT 07:25pm
  3. @Chris as cool as this is I just find the whole thing a mess when it comes to visually following all the replies that can occur.

    wrote Marcus on Fri, June 12, 2009 AT 02:10pm
  4. @Nick Kutateli I had a feeling it was a problem with the HTML settings. Thanks for the quick reply, Nick!

    I was trying to muck about with making a plugin to do this but since you posted the thread on the EE Forums, I’m quite happy to see it can be done with far less code and no need for a plugin. Thanks very much for posting this!

    Cheers,
    Chris

    wrote Chris on Tue, June 02, 2009 AT 02:23pm
  5. @Chris
    You have to allow all HTML in your comment posting preferences. A secondary option would be to allow the “cite” and “a” tags in the “safe HTML” list which can be edited in system/core/core.typography.php lines 60-80.

    wrote Nick Kutateli on Tue, June 02, 2009 AT 02:04pm
  6. One problem I seem to be having with it is that the anchor link generated by the reply button doesn’t amend the full URL to the comment once it is submitted and displayed. So, instead of getting http://url.com/article/comments/#c_1 it is showing up as http:///#c_1

    I was wondering if I have to set the Comment Posting Preferences for the blog to anything special.

    wrote Chris on Tue, June 02, 2009 AT 01:30pm

Leave a Comment