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 ;)
Just clone the container whose nodes you want to copy and get the nodes from the copied one, if you want to make the original container intact. And you are putting them into the array and then running a loop multiple times instead you can run a loop once and simultaneously put them in your .
Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.
To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.
The innerHTML property is a part of the Document Object Model (DOM) that is used to set or return the HTML content of an element.
innerHTML
doesn't insert DOM nodes, just strings.
Use appendChild
instead
var box1 = document.querySelector(".box1");
var box2 = document.querySelector(".box2");
box2.appendChild( box1 );
You should use the appendChild method.
box2.appendChild(box1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With