4

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

Ivan Chernykh
  • 40,230
  • 12
  • 130
  • 145
MyruBapa
  • 138
  • 2
  • 6

2 Answers2

17

use outerHTML

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

Demo: Fiddle

Arun P Johny
  • 376,738
  • 64
  • 519
  • 520
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;
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111