179

How do i get the html on '#container' including '#container' and not just what's inside it.

<div id="container">
 <div id="one">test 1 </div>
 <div id="two">test 2 </div>
 <div id="three">test 3 </div>
 <div id="four">test 4 </div>
</div>

I have this which gets the html inside #container. it does not include the #container element itself. That's what i'm looking to do

var x = $('#container').html();
$('#save').val(x);

Check http://jsfiddle.net/rzfPP/58/

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Pinkie
  • 10,126
  • 22
  • 78
  • 124
  • you could put container inside another container and get that containers html... but that seems a little hacky. perhaps if we knew a little more about the problem, we could come up with a workable solution? what are you doing with a text area full of html? – Patricia Jun 23 '11 at 19:18
  • possible duplicate of [Get selected element's outer HTML](http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html) – Ian Mackinnon Apr 10 '15 at 10:15

10 Answers10

176

If you wrap the container in a dummy P tag you will get the container HTML also.

All you need to do is

var x = $('#container').wrap('<p/>').parent().html();

Check working example at http://jsfiddle.net/rzfPP/68/

To unwrap()the <p> tag when done, you can add

$('#container').unwrap();
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Hussein
  • 42,480
  • 25
  • 113
  • 143
  • 21
    @mc10 we can simply use clone() and you will not have to worry about extra elements created. `var x = $('#container').clone().wrap('

    ').parent().html();`. The idea of wrap is great and allot less complicated then most of the solutions provided.
    – Pinkie Jun 24 '11 at 00:18
  • Firefox issue is out-of-date so I suggest to vote up @MikeM answer because it's in pure JS. – Rob Dec 16 '13 at 12:00
  • 6
    Why a `

    ` tag? Wouldn't a `

    ` make more sense?
    – Martin Burch Jun 12 '14 at 23:26
  • 7
    This has a much simpler solution. See my answer. – tozlu Sep 02 '15 at 10:34
150
var x = $('#container').get(0).outerHTML;

UPDATE : This is now supported by Firefox as of FireFox 11 (March 2012)

As others have pointed out, this will not work in FireFox. If you need it to work in FireFox, then you might want to take a look at the answer to this question : In jQuery, are there any function that similar to html() or text() but return the whole content of matched component?

Community
  • 1
  • 1
ShaneBlake
  • 11,056
  • 2
  • 26
  • 43
107

I like to use this;

$('#container').prop('outerHTML');
tozlu
  • 4,667
  • 3
  • 30
  • 44
72
var x = $('#container')[0].outerHTML;
MikeM
  • 27,227
  • 4
  • 64
  • 80
  • 1
    Firefox issue is out-of-date so I suggest to vote up this answer. – Rob Dec 16 '13 at 11:59
  • 1
    Using .clone() works, but this is much cleaner, imo. The accepted answer creates new elements in the DOM = bad. – pete Mar 15 '14 at 20:30
16
$('#container').clone().wrapAll("<div/>").parent().html();

Update: outerHTML works on firefox now so use the other answer unless you need to support very old versions of firefox

Robert Noack
  • 1,286
  • 14
  • 22
2

Oldie but goldie...

Since user is asking for jQuery, I'm going to keep it simple:

var html = $('#container').clone();
console.log(html);

Fiddle here.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
prinsen
  • 955
  • 8
  • 7
  • 2
    This does not help in retrieving the html of the container itself. It does not even return html code. I can see that it may be practical to access the target node via clone to avoid modifying the dom, but then it would be good to mention why you use this method. – AeonOfTime Jul 24 '18 at 07:11
1
$.fn.outerHtml = function(){
    if (this.length) {
        var div = $('<div style="display:none"></div>');
        var clone =
        $(this[0].cloneNode(false)).html(this.html()).appendTo(div);
        var outer = div.html();
        div.remove();
        return outer;
    }
    else {
        return null;
    }
};

from http://forum.jquery.com/topic/jquery-getting-html-and-the-container-element-12-1-2010

YJM
  • 133
  • 1
  • 2
  • 11
0
var x = $($('div').html($('#container').clone())).html();
animuson
  • 53,861
  • 28
  • 137
  • 147
kei
  • 20,157
  • 2
  • 35
  • 62
0

Firefox doesn't support outerHTML, so you need to define a function to help support it:

function outerHTML(node) {
    return node.outerHTML || (
        function(n) {
            var div = document.createElement('div');
            div.appendChild( n.cloneNode(true) );
            var h = div.innerHTML;
            div = null;
            return h;
        }
    )(node);
}

Then, you can use outerHTML:

var x = outerHTML($('#container').get(0));
$('#save').val(x);
Community
  • 1
  • 1
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
-2

Simple solution with an example :

<div id="id_div">
  <p>content<p>
</div>

Move this DIV to other DIV with id = "other_div_id"

$('#other_div_id').prepend( $('#id_div') );

Finish

  • The OP is requesting the raw HTML represented by a jQuery object, where this method moves a jQuery object from one place to another. – Simon Robb Nov 21 '13 at 01:56