jQuery Pull Entire DIV into String Variable?
Solution 1
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/
Solution 2
Finally got it working..!! the simplest way !!
HTML Code
<div class="extra-div">
<div class="div-to-pull-in-variable">
// Your Content Here
</div>
</div>
JQuery Code
$(function() {
var completeDiv = $('.extra-div').html();
alert(completeDiv);
})
});
Solution 3
you can get the parent...
<div class='a'>
blah
</div>
....
alert(jQuery('.a').parent().html())
Craig
Updated on June 27, 2022Comments
-
Craig almost 2 years
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?