I have created a script that copies the entire div into another div successfully.
Check out the code below:
Script:
var x = 1;
function duplicate()
{
var firstContent = document.getElementById('1');
var secondContent = document.getElementById('2');
if(x <=10)
{
x++;
var targetDiv = document.getElementById('m')
var newDiv = document.createElement("div");
newDiv.innerHTML = (secondContent.innerHTML = x + firstContent.innerHTML);
targetDiv.appendChild(newDiv);
}
}
HTML:
<input type="button" onclick="duplicate();" value="+" />
<div id="m">
<div id="1">
test
</div>
<div id="2"></div>
</div>
Although the code works, it keeps duplicating the content above like this:
OUTPUT:
1st OUTPUT:
test
2 test
2 test
2nd OUTPUT:
test
3 test
2 test
3 test
EXPECTED OUTPUT:
1st OUTPUT:
test
2 test
2nd OUTPUT:
test
2 test
3 test