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

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
MyruBapa
  • 148
  • 1
  • 2
  • 6

2 Answers2

21

use outerHTML

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

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
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
  • 795,719
  • 175
  • 1,089
  • 1,143