As shown in this simple jsfiddle, the following code outputs the <p>I am inner content</p>
but seems to drop the div tag. What am I missing?
var x = $('<div id="#testing"><p>I am inner content</p></div>');
alert(x.html());
As shown in this simple jsfiddle, the following code outputs the <p>I am inner content</p>
but seems to drop the div tag. What am I missing?
var x = $('<div id="#testing"><p>I am inner content</p></div>');
alert(x.html());
Because .html() returns the innerHTML of x
.
Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
Youn eed to get the outerHTML
var log = (function() {
var $log = $('#log');
return function(msg) {
$('<p/>', {
text: msg
}).prependTo($log)
}
})();
var x = $('<div id="#testing"><p>I am inner content</p></div>');
log(x.prop('outerHTML'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="log"></div>
Try this fiddle
var x = $('<div id="#testing"><p>I am inner content</p></div>');
alert(x.prop('outerHTML'));
outerHTML is similar to innerHTML, it is an element property that includes the opening an closing tags as well as the content.