Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Pull Entire DIV into String Variable?

Howdy, I'm a little confused about how to work with this in jQuery (1.5.2).

For a nested comment thread I'm navigating to a parent comment's reply link. I'm attempting to capture it, manipulate the string, and spit it back out elsewhere on the page.

This is how I'm traversing the tree to the target:
$('.commentlist > li > div .reply').each(function() {

At this point I can gobble up this entire div by using var reply = this; -- and that works great if I was simply cloning it -- but I honestly don't know how to take that array of information and pluck just the complete HTML string out of it.

Thoughts?

UPDATE:

Hi again. The goal is to copy this fellow into a simple string variable for each match:

<div class="reply">
<a class="comment-reply-link" href="http://someurl/foo/testing-post-four/?replytocom=5#respond" onclick='return addComment.moveForm("comment-5", "5", "respond", "8")'>Reply</a>
</div>

Using alert ($(this).html()); only returns the inner anchor tag & contents, not the div wrapper. Thoughts?

like image 417
Craig Avatar asked May 02 '26 16:05

Craig


1 Answers

You could clone it into another <div> and then call html() on that:

var html = $('<div/>').append($(this).clone()).html();

For example: http://jsfiddle.net/ambiguous/59zwr/1/

like image 141
mu is too short Avatar answered May 05 '26 06:05

mu is too short