Paste outer html

1

How do I get the outerHTML of a jquery object? The following form is not working.

$("selector").outerHTML;
    
asked by anonymous 12.05.2015 / 16:50

2 answers

4

A jQuery object has a reference to the native element you want and you can extract it with $('selector')[0] , from there you can use native JS.

var outer = $('selector')[0].outerHTML;

Another option is to create a temporary element that inserts the first element and searches for .html() . You may need this solution if you have multiple elements.

var outer = $('<div>').append($('selector').clone()).html();

or, without having to clone:

var outer  = $('selector').wrapAll('<div>').parent().html();
    
12.05.2015 / 18:02
1

Use

$("selector")[0].outerHTML;
    
12.05.2015 / 16:54