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.

4

7 Answers 7

6

you could do something like this:

alert( $('#\\31 ').wrap("<div />").parent().html() )
$('#\\31 ').unwrap()
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, to select the element with id=1 you would need to use $('#\\31 '), not $('#1'). See mothereff.in/css-escapes#01. (I know it’s just an example, but hey, it doesn’t work.)
@MathiasBynens thx for this, i was still used to ID's being not valid as numbers froum HTML4 ;)
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 .

6 Comments

string outer and not var outer? Learning something new every day..!
@ChrisKempen, no, var outer is correct. Sometimes my brain thinks C# when writing JS.
Haha no probs! Liking your solution, +1!
It happens with everyone ;) I've just seen a foreach in javascript :)
|
2

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

/*! Copyright (c) 2006 Brandon Aaron ([email protected] || 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();

1 Comment

this is a pretty clean solution that only takes more effort for lame browsers... +1
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

2 Comments

This doesn't work in firefox (possibly some other browsers too)
Although just checked, and it looks like a release with outerHTML implemented is fairly imminent bugzilla.mozilla.org/show_bug.cgi?id=92264
0

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

1 Comment

..but have a close look on the browser compatibility table.
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)

Comments

-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.

3 Comments

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.
I mentioned you 'can'. If you cant , dont use it . Keep it simple :)
Also, who told you to change the markup, we have clone(). See the second answer, stackoverflow.com/questions/2419749/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.