Jareddlc
Jareddlc

Reputation: 349

jquery how to add comments to a post

Hello I'm currently making a "forum" in which a user can submit a post and will be displayed. I would like the option to add a comment to it.

I am doing everything client side.

Here is the code to generate the forum:

$.get(url, page, function(data){
      $("#forum").html("");
      for(var i in data)
      {
        $("#forum").append(buildForumEntry(data[i]));
      }
    });

// Builds the forum entry
  function buildForumEntry(post)
  {
    var titleAndType = '<div><span class="forum-title">'+post.title+'</span>'+buildType(post.type)+'</div>';
    var author =  "<div class=\"forum-author\">By: "+post.author+" on "+formatDate(post.date)+"</div>";
    var body =    "<pre>"+post.body+"</pre>";
    var comment = "<div class=\"forum-comment\"> <div class=\"btn-group\"><a class=\"btn btn-mini btn-primary\" role=\"button\" data-toggle=\"modal\" id=\"btn-forum-comment\"><i class=\"icon-comment icon-white\"></i> comment</a></div></div>";
    var footer =  "<hr style=\"border-top: 1px dotted #b0b0b0;border-bottom: 0px\">";
    var entry = titleAndType+author+body+comment+footer;
    return entry;
  }

How could I do it so when I click on the comment btn I grab some information of the post? Also in this method calling:

// Button comment
  $("#btn-forum-comment").click(function() {
    alert("clicked");
  });

This wont work because there are more than 1 types of "id=btn-forum-comment". Should I use a class? If so how do I grab more instance the title of the post so that when I post to my server I can update the correct entry.

UPDATE: class isn't working from the generated forum. The button click isn't getting triggered. Any ideas?

var comment = '<div class="btn-group"><a class="btn btn-mini btn-primary btn-forum-comment" id="btn-forum-comment-'+post._id+'"><i class="icon-comment icon-white"></i> comment</a></div>';

// Button comment
  $(".btn-forum-comment").click(function() {
    // now you know the id of clicked comment, so you can rewrite this with code which will open a form to answer exactly this comment
    console.log("clicked");
    alert($(this).attr('id')+" clicked"); 
  });

I am having issues with the click, I think it has to do something with the dynamic loading. I apply the class to an hardcoded html and it works. But not with the dynamic thanks.

UPDATE2: updating for future users. I needed to use the delegate function

$("#forum").delegate(".btn-forum-comment", "click", function() {
    console.log("clicked");
    alert($(this).attr('id')+" clicked");     
  });

Upvotes: 1

Views: 2882

Answers (3)

Jeff M
Jeff M

Reputation: 477

yes, use a class.

also, you should wrap each entry inside a container div. this will help you access other parts of the forum entry if need be.

html (condensed for brevity):

<div class="forum-entry" data-id="1234">
  <div><span class="forum-title">blah blah</span>...</div>
  <pre>post body...</pre>
  <div class="forum-comment">
    <div class="btn-group">
      <a class="btn btn-mini btn-primary btn-forum-comment">comment</a>
    </div>
  </div>
  <hr />
</div>

javascript:

$('.btn-forum-comment').click(function(ev) {
    ev.preventDefault();
    var $forumEntry = $(this).closest('.forum-entry'); // this will get you a handle to the forum entry.  from there, you can access other parts of the entry
    var forumEntryId = $forumEntry.data('id');

    alert('add a comment to forum entry ID ' + forumEntryId);
    // etc...
});

Upvotes: 1

Khanh TO
Khanh TO

Reputation: 48972

Add a class to your button:

<a class=\"btn btn-mini btn-primary btn-forum-comment\" ... id=\"btn-forum-comment-"+post.id+"\">

and a class to the post

  var entry = "<div class='post'>"+ titleAndType+author+body+comment+footer +  "</div>"; 

Your event handler:

$(".btn-forum-comment").click(function() {
    var post = $(this).parents(".post"); //the post of this clicked button
    //from there you can access title, author,... of this post.
    var title = post.find(".forum-title");
  });

With this approach, you will be able to access the correct title, author,.. of the clicked button.

Upvotes: 1

dimaninc
dimaninc

Reputation: 850

you should use ID of the post in btn's id, and your post object should have such a field post.id (it will contain the unique id of the post in database)

also use the class to access all the buttons

<a class=\"btn btn-mini btn-primary btn-forum-comment-class\" ... id=\"btn-forum-comment-"+post.id+"\">

and the click event:

// Button comment
  $(".btn-forum-comment-class").click(function() {
    // now you know the id of clicked comment, so you can rewrite this with code which will open a form to answer exactly this comment
    alert($(this).attr('id')+" clicked"); 
  });

Upvotes: 1

Related Questions