Forum Moderators: open

Message Too Old, No Replies

jQuery - append() a link with values based on id from parents()

I can append the link, and get the value, but having trouble combining them

         

illtron

10:22 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



First, some background: I'm trying to create links for replying to replies in a Movable Type-powered forum. I'm going for a similar effect to Digg's comments, where after two replies, they become flat.

To do this, I need to get the ID of the parent comment. You can see below that I'm able to get it, and it gives me the right value when I alert it. The problem is getting that ID into the onclick for the "reply to thread" link.

Each page will have multiple instances of this, so I need to take that into account.

Thanks!

$(document).ready(function(){
$('.sub .reply-link').append('<a href="javascript:void(0);"' + '" onclick="mtReplyCommentOnClick(' + '2633' + ',' + '\'user name\')">Reply to thread</a>'); // 2633 is a dummy value. I need to replace it with value of replyToId.

$('.reply-link a').click(function(){ // This is just in the click event because I'm not sure how to do it when the page loads.
var parentCommentId = $(this).parents('div.comment').attr("id"); // Get the ID of the div containing the parent comment
var replyToId = parentCommentId.replace(/comment-/, '') // This gives me the same value as the Comment's ID within Movable Type
alert(replyToId); // Just for testing - Fire an alert to make sure I'm getting the ID
});
});

daveVk

10:17 am on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this refactoring correct ?

$(document).ready(function(){

var parentCommentId = $('.reply-link a').parents('div.comment').attr("id");
var replyToId = parentCommentId.replace(/comment-/, '')

$('.sub .reply-link').append('<a href="javascript:void(0);"' + '" onclick="mtReplyCommentOnClick(' + replyToId + ',' + '\'user name\')">Reply to thread</a>');

daveVk

10:00 am on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On second thought, seeing there are multiples

$(document).ready(function(){
$('.sub .reply-link').each( function(){
var replyToId = ... code based on $(this) ...
$(this).append('<a href="javascript:void(0);"' + '" onclick="mtReplyCommentOnClick(' + replyToId + ',' + '\'user name\')">Reply to thread</a>');
} ) // end each
) } // end ready

illtron

4:25 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



You nailed it on the second one. each() is exactly what I needed! Thanks for helping me out. It turned out to be much easier than I was thinking!