AJAX - And using .data()

AJAX - And using .data()

Brief description of what I'm attempting to do: 

I'm developing a small web application where I'm posting comments with AJAX. So I'm using the $.post method and jQuery templates to do that. All is fine there. However, I'm also attempting to make a like button where the user should be able to like each post. 

So my first problem is that I need to store the comment ID in the like link somehow (I think), please correct me if I'm wrong. So the best way I could conceive of to do that was to use the .data() function in jQuery. 

So what I do to post the posts and add the comment ID to the like link is:
  1.                 $.post("/Home/Create", comment, function (data) {
  2.                     var totalItems = data.length;
  3.                     var currentItems = $(".list-group-item").size(); /

  4.                  
  5.                     //In data is a list of all the posts, so I need to loop through the new ones and add them
  6.                     for (var i = currentItems; i < totalItems; i++) {
  7.                         $("#CommentList").loadTemplate($("#CommentTemplate"), {
  8.                             Username: data[i].Username,
  9.                             CommentText: data[i].CommentText,
  10.                             CommentDate: data[i].CommentDate
  11.                         }, { append: true });
  12.                         //Not sure how to add .data() to an item through loadTemplate, so I just select the last (newest) item in the list and add it below
  13.                         var temp = $(".list-group-item").last();
  14.                         temp.data("comment", data[i].ID);
                        alert(temp.data("comment")); //Works

The reason for why I'm attempting to add the commentID to the anchor tag is so I can use the on click event of the anchor tag and reach the commentID somehow, so I can save the like for that comment. 

Is there another (much better?) way to do this? 

Where I run into problems is, in the on click event, I try to write out $(this).data("comment"), I get the value undefined. I'm not sure why.

Here's the onclick event:

  1. $(document).on("click", ".like-comment", function () {
  2.                 alert($(this).data("comment")); //Writes out undefined for some reason..
  3.             });