The jQuery .html()
returns the HTML code inside the selected DOM element, but not the element's HTML. That is why you are getting an empty string, because there is no HTML inside your image element. jQuery doest not provide a solution for this, but you can use outerHTML at the DOM element (not the jQuery one). If you want to do this with jQuery, create an element, put the one you want inside of it, and then use .html()
. Look:
var string = '<img src="a.jpg"/>';
var $el = $(string);
var $wrapper = $('<p>').append($el).html();
Hope I've helped.