I'm trying to copy individual divs created dynamically. I'm using the javascript from this question which works a treat. However, the user should be able to copy any of the dynamically created divs. Unfortunately at the moment only the last div is copied no matter which button is clicked.
Here's my code...
<?php $ii=0; //set counter
foreach($bin_question_data as $k=>$v){ //loop through each section to create div ?>
<div id="duplicater<?php echo $ii;//use counter to create individual div id ?>">
<h4><?php echo $v[0]['bintype'];?></h4>
<?php foreach($v as $vv){ //loop through section elements to display questions?>
<?php echo $bin_question=$vv['binquestiontext'];
$questionid=$vv['binquestionid'];?>
<!--questions are dynamically created here-->
} //end loop through section questions ?>
</div><!-- end div to be duplicated-->
<!--Add button to duplicate preceding div-->
<p><input type="button" name="add_bin"
id="add_bin<?php echo $ii;?>" onlick="duplicate()" value="Add <?php echo $v[0]['bintype'];?> bin" /></p>
<!--script to carry out duplication-->
<script type="text/javascript">
document.getElementById('add_bin<?php echo $ii;?>').onclick = duplicate;
var i = 0;
var original = document.getElementById('duplicater<?php echo $ii;?>');
function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicetor" + ++i; // there can only be one element with an ID
original.parentNode.appendChild(clone);
}
</script>
<?php $ii++; //increase counter
}//end original section loop ?>
Any hints as to where I'm going wrong or what I've missed will be gratefully received!
Many thanks in advance for your help.