I want a div to be duplicated when a button is clicked. I though something like this; but it's not working. Can anyone help me?
HTML
<div id="duplicater">
duplicate EVERYTHING INSIDE THIS DIV
</div>
JAVASCRIPT
function duplicate()
{
var div = duplicate("div");
div.id = "duplicater";
div.appendChild(duplicate("duplicater"));
}
You are creating an infinite recursion!
The function is calling itself over and over again. Use
cloneNode()
:HTML:
JavaScript:
Working DEMO
Or without IDs:
Update:
If you want to clone the div on button click, you can use a slightly different version:
HTML:
JavaScript:
If you are not in a form, you should use
<button>
instead of<input type="button">
.Working DEMO 2
Can you change the div to a class? If so, then this might work for you:
Edit the code here: