Atomix
Atomix

Reputation: 2500

document.getElementById("images").children[0] to a string

The toString() method will output [object HTMLImageElement]. I want a string representation of the the image element '<img src="..." />'. outerHTML returns undefined in firefox.

How can I accomplish this?

Upvotes: 3

Views: 10309

Answers (1)

lonesomeday
lonesomeday

Reputation: 237865

outerHTML is not cross-browser.

The easiest way is to clone the element and add it to a parent element, then get the innerHTML of that:

var outer = document.createElement('outer'),
    child = document.getElementById(“images”).children[0].cloneNode(true);

outer.appendChild(child);

var imgHtml = outer.innerHTML;

Upvotes: 11

Related Questions