innerHTML inserts only [object HTMLDivElement]
Asked Answered
E

3

12

i want to insert a htmlsnippet into another html-element

i tried it this way

the html

<div class="box1">
    insert this html element
</div>
<div class="box2">
  into this
</div>

the js

var box1 = document.querySelectorAll(".box1")[0]; 
var box2 = document.querySelectorAll(".box2")[0];
console.log(box1);
box2.innerHTML = box1;

but it doesnt work it only insert [object HTMLDivElement], if i look at the console it puts out the correct html, what im doing it wrong?

and yeah i dont want to use the $ libary ;)

http://codepen.io/destroy90210/pen/MKQOwy

thanks ;)

Enate answered 23/1, 2016 at 16:8 Comment(0)
C
23

innerHTML doesn't insert DOM nodes, just strings.
Use appendChild instead

var box1 = document.querySelector(".box1"); 
var box2 = document.querySelector(".box2");

box2.appendChild( box1 );
Cox answered 23/1, 2016 at 16:10 Comment(0)
H
1

You should use the appendChild method.

box2.appendChild(box1);
Hoofbeat answered 23/1, 2016 at 16:12 Comment(0)
L
1

This should work:

box2.appendChild(box1);
Lymphadenitis answered 23/1, 2016 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.