Home > Enterprise >  Why am I getting [object HTMLDivElement] instead of the actual content?
Why am I getting [object HTMLDivElement] instead of the actual content?

Time:01-29

Hi,

I have this code:

var div = document.createElement('div');

cont.outerHTML=div; //tried with innerHTML too but didnt work either

but instead of getting a <div></div> it outputs this: [object HTMLDivElement]

I also tried:

div.innerHTML = '<div>hit1</div><div>hit2</div><div>hit3</div>';

but same result. Why is this happening?

Thank you.

CodePudding user response:

[object HTMLDivElement] is the result of div.toString() and is the default serialization of an HTML element.

If you want to add a child element to a container please try:

var div = document.createElement('div');

cont.appendChild(div);
  • Related