6

I don't need innerHTML i need innerHTML with enclosing tags. Lets write some example:

<div id="1" style="qwe"><span class="1"></span></div>
<div id="2" style="asd"><span class="2"></span></div>
<div id="3" style="zxc"><span class="3"></span></div>

I can get element by id:

$("#1")

And how can i get string like that:

<div id="1" style="qwe"><span class="1"></span></div>

Of course html() doesn't work becouse it will return only span.

Fisher
  • 1,602
  • 1
  • 18
  • 36
  • 1
    possible duplicate of [JQuery Object to String](http://stackoverflow.com/questions/652763/jquery-object-to-string) – Madara's Ghost Feb 07 '12 at 12:50
  • possible duplicate of [Get selected element's outer HTML](http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html) – Amar Palsapure Feb 07 '12 at 12:52
  • possible duplicate of http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html – bang Feb 07 '12 at 12:54
  • Actually, to select the element with `id=1` you would need to use `$('#\\31 ')`, not `$('#1')`. See http://mothereff.in/css-escapes#01. – Mathias Bynens Feb 08 '12 at 21:07

7 Answers7

6

you could do something like this:

alert( $('#\\31 ').wrap("<div />").parent().html() )
$('#\\31 ').unwrap()
meo
  • 29,963
  • 17
  • 83
  • 122
  • 2
    Actually, to select the element with `id=1` you would need to use `$('#\\31 ')`, not `$('#1')`. See http://mothereff.in/css-escapes#01. (I know it’s just an example, but hey, it doesn’t work.) – Mathias Bynens Feb 08 '12 at 21:08
  • @MathiasBynens thx for this, i was still used to ID's being not valid as numbers froum HTML4 ;) – meo Feb 09 '12 at 15:00
5

Something like this should work fine:

jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};

var outer = $("#1").outerHTML();

Here's a working fiddle.

Additional Info

See http://www.yelotofu.com/2008/08/jquery-outerhtml/ for original article .

wheresrhys
  • 21,254
  • 18
  • 86
  • 158
James Hill
  • 58,309
  • 18
  • 142
  • 160
2

Use this jQuery plugin: https://github.com/brandonaaron/jquery-outerhtml/blob/master/jquery.outerhtml.js

/*! Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 */

(function($){
  var div;

  $.fn.outerHTML = function() {
    var elem = this[0],
      tmp;

    return !elem ? null
      : typeof ( tmp = elem.outerHTML ) === 'string' ? tmp
      : ( div = div || $('<div/>') ).html( this.eq(0).clone() ).html();
  };

})(jQuery);

Use it as follows:

$('#some-element').outerHTML();
Mathias Bynens
  • 137,577
  • 52
  • 212
  • 242
  • 1
    this is a pretty clean solution that only takes more effort for lame browsers... +1 – meo Feb 07 '12 at 13:28
1

You can use outerhtml but in JavaScript over the DOM and not jQuery, for example:

  var text = document.getElementById('hello').outerHTML;

jsbin code to demonstrate: http://jsbin.com/obutul/edit#javascript,html,live

Ben
  • 9,510
  • 21
  • 90
  • 153
0

There is also an outerHTML property on html elements, which includes the selected element itself.

Sirko
  • 69,531
  • 19
  • 142
  • 174
0

outerHTML is implemented across nearly all browsers now (including old versions of ie - firefox is the only one dragging its feet, but it's scheduled for v11), so I've adapted @James Hill's answer to use this native functionality where present as it should be more efficient.

jQuery.fn.outerHTML = function(s) {
    return this[0].outerHTML ? this[0].outerHTML :
           s ? this.before(s).remove()
             : jQuery("<p>").append(this.eq(0).clone()).html();
};

var outer = $("#1").outerHTML();

Be aware though that there are a few cross-browser inconsistencies in outerHTML (e.g look at this page in chrome and compare with ie)

wheresrhys
  • 21,254
  • 18
  • 86
  • 158
-1

You can wrap the desired div in another div and then fetch the parent div's html.

<div><div id="1" style="qwe"><span class="1"></span></div></div>
<div><div id="2" style="asd"><span class="2"></span></div></div>
<div><div id="3" style="zxc"><span class="3"></span></div></div>

Now,

$("#1").parent().html() will fetch the desired string.

Jashwant
  • 27,289
  • 16
  • 69
  • 100
  • Sorry - a bit slow to comment. This is slightly unworkable because it either a) involves changing the html in advance, so can't be used on any arbitrary element; or b) means you have to inject a div into the DOM using JS, which might mess with other code which expects a different DOM tree. – wheresrhys Feb 07 '12 at 13:03
  • I mentioned you 'can'. If you cant , dont use it . Keep it simple :) – Jashwant Feb 07 '12 at 13:06
  • Also, who told you to change the markup, we have clone(). See the second answer, http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html – Jashwant Feb 07 '12 at 13:13