User123 User123 - 1 month ago 5
jQuery Question

How to get raw html passed as jQuery selector?

Note: Answer given by SLacks requires the least code to get what I want compared to answers in the duplicate questions. Accepting this.

I'm trying to do get the raw html that's passed in as the selector:

$('<div></div>')


I need just the html so
'<div></div>'


Thanks!

Answer

You're looking for the native outerHTML property: el[0].outerHTML

Note: this is not quite "raw"; this is the HTML as parsed by the browser.

example of parsed output:

$("<div></div>")[0].outerHTML = "<div></div>";    
$("<div/>")[0].outerHTML = "<div></div>";
Comments