I have created a form where it duplicates itself however I am unable to make the 'x' button remove the div it represents as required.
I placed both my buttons out of the div as shown below:
<button type="button" id="cross" class="buttonImgTop" onclick="remChild()"></button>
<div id="ValuWrapper"> ...content comes here... </div>
<button type="button" class="buttonImg" onclick="repeat()"></button>
The 'x' button and 'div' get cloned and duplicated every time the '+' sign is clicked to add more forms on the website.
Here is the code for both cloning the form and removing it:
<script>
var i = 0;
var original = document.getElementById('ValuWrapper');
var crossButton = document.getElementById('cross');
var n = 0;
function repeat() {
var clone = original.cloneNode(true);
var crossBut = crossButton.cloneNode(true);
clone.id = "ValuWrapper" + ++i;
crossBut.id = "cross" + i;
crossButton.parentNode.appendChild(crossBut);
original.parentNode.appendChild(clone);
// used for remChild() function
n = i;
}
function remChild(){
for(i = 0; i <= n; i +=1)
{
$("#cross"+[i]).click(function () {
$("#ValuWrapper"+[i]).slideUp(400, function () {
$("#ValuWrapper"+[i]).remove();
$(this).remove();
});
});
}
}
</script>
What I am trying to do is when the 'x' button is clicked, the animation 'slideUp()' works on the specified div then removes both the button and div and this should happen in any order the client would like. However it seems like it is not working.