5

it is impossible to get a string of createElement that you would assign to a variable

var h1 = document.createElement("h1")
h1.innerHTML = "hello world"
alert(h1)
return "[object HTMLHeadingElement]"

when i use appendChild is work but i must use alert or other method

2
  • Why can't you just alert(h1.innerHTML)? Commented Aug 31, 2013 at 7:46
  • may be h1.toString() ? Commented Aug 31, 2013 at 7:49

2 Answers 2

21

use outerHTML

var h1 = document.createElement("h1")
h1.innerHTML = "hello world"
alert(h1.outerHTML)

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

I always forget about that. I think I'm still stuck at FF 4 ;)
0

It seems like you want to convert a DOM element to its HTML representation. Put it in a temporary container and access its .innerHTML property:

var div = document.createElement('div');
div.appendChild(h1);
var html = div.innerHTML;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.