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. I figured it out, but I prefer not to allow html within comments. So I replaced the < cite > tags you used in your example with ordinary [ quote ]’s from ExpressionEngine. Users know how to read these and I don’t want to confuse them with new tags.

    As a result I’ve got to learn to live with the fact multiple quotes will kind of break the quotes. Perhaps there are difficult solutions to this issue, but I can’t find the time to look into it. I don’t expect you to do so either, so don’t worry too much. What you created is awesome!

    wrote Ron on Thu, May 27, 2010 AT 11:41am
  2. Hi @Ron,

    Thanks for the spotting the problem! The issue is happening because the script is grabbing the text from what you are quoting, and not the html. Because of this, it’s not grabbing the “cite” tags and isn’t nesting the quote within the quote.

    The line of js where this happens is:
    var quote = $(this).parents("li").find(’p’).text();

    You can try some fixes by using .html() instead of .text(). If you can’t figure it out, shoot me an email at nick@kutateli.com and I’ll try to get it working on your site.

    Cheers,
    Nick

    wrote Nick Kutateli on Thu, May 27, 2010 AT 10:43am
  3. And I’m guessing you’re running into the same problem as I was… This is the third and final comment with nested quotes:

    @Ron“Here’s the second post with one quote:

    @Ron“Sorry for testing this. If you want to remove these comments, I don’t mind.”

    wrote Ron on Thu, May 27, 2010 AT 10:23am
  4. Here’s the second post with one quote:

    @Ron
    Sorry for testing this. If you want to remove these comments, I don’t mind.

    wrote Ron on Thu, May 27, 2010 AT 10:22am
  5. I just came across this cool implementation but I seem to be running into a problem when there are multiple quotes. Wondering how you solved it or if the problem exists here as well…

    Sorry for testing this. If you want to remove these comments, I don’t mind.

    Anyway: thanks for the cool implementation! It looks pretty awesome!

    wrote Ron on Thu, May 27, 2010 AT 10:21am
  6. Right on Nick, i knew the html setting were playing a role here.. Thanks for adding this information its been of great help..@Nick Kutateli

    wrote Jordan on Wed, February 24, 2010 AT 05:20am
  7. @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
  8. @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
  9. @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
  10. @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
  11. @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
  12. 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