Skip to content Skip to sidebar Skip to footer

How To Get The InnerHtml, Including The Tag, Using JQuery?

Sorry, if the title is too obscure ;D. Actually the problem is, lets say i am this code. code here sample

Solution 1:

This will create a new div and append a clone of your element to that div. The new div never gets inserted into the DOM, so it doesn't affect your page.

var theResult = $('<div />').append($("#spanIDxx").clone()).html();

alert( theResult );

If you need to use this frequently, and don't want to bother with adding yet another plugin, just make it into a function:

function htmlInclusive(elem) { return $('<div />').append($(elem).clone()).html(); }

alert( htmlInclusive("#spanIDxx") );

Or extend jQuery yourself:

$.fn.htmlInclusive = function() { return $('<div />').append($(this).clone()).html(); }

alert( $("#spanIDxx").htmlInclusive() );

Solution 2:

You can copy the node to a new empty node, ask for new .parent() and then its .html()


Solution 3:

Clone might be useful to you. I don't believe you actually need to do anything with the clone/s.


Solution 4:

Haven't used this myself but looks like it may be of use:

http://yelotofu.com/2008/08/jquery-outerhtml/

demo here: http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html


Post a Comment for "How To Get The InnerHtml, Including The Tag, Using JQuery?"