tags:

views:

433

answers:

3

Is there a good way of printing out a jQuery object as pure HTML?

ex:

<img src="example.jpg">

<script>
var img = $('img').eq(0);
console.log(img.toString());
</script>

toString() doesn't work this way. I need the HTML equivalent string , i.e:

<img src="example.jpg">

A: 

You could wrap it and then use html:

var img = $('img').eq(0);
console.log(img.wrap("<span></span>").parent().html());
Magnar
works, but looks kind of awkward... is there a better way?
David
+1  A: 

If you need to print the object in HTML format, use this extension outerHTML.

andres descalzo
so, I really need an extension for this? it does the same as Magnar's example, except cloning before parsing.
David
if the same thing, but you need to clone him to keep moving the elements in the DOM, and them remove this.
andres descalzo
A: 

You can use the following piece of code.

var str = $("img").html();

Gunner 4 Life
No, that would return an empty string, since `html` returns innerHTML.
Magnar