Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  60] [ 5]  / answers: 1 / hits: 27994  / 9 Years ago, sat, january 23, 2016, 12:00:00

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 ;)


More From » innerhtml

 Answers
7

innerHTML doesn't insert DOM nodes, just strings.

Use appendChild instead



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

box2.appendChild( box1 );

[#63587] Friday, January 22, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elmer

Total Points: 432
Total Questions: 96
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
;