I was able to successfully clone and remove certain part of my form thanks to David Carr - Duplicate form sections with jQuery
Now I have been trying to change two div
ids (#ifYes
& #ifNo
) which are hidden
to provide them with a unique id every time the form is cloned, I have added two line of coding to change the div ids which doesn't really work.
My code is as follows:
HTML:
<div id="content">
<button type="button" id="cross" class="buttonImgTop cross"></button>
<div id="ValuWrapper">
<div id="ifYes"> .. </div>
<div id="ifNo"> .. </div>
</div>
</div>
<button type="button" class="buttonImg" id="repeat"></button>
JavaScript:
//define template
var template = $('#content:first').clone();
//define counter
var count = 0;
//add new section
$('body').on('click', '#repeat', function() {
//increment
count++;
//remove cross button div
template.clone().find('cross').removeAttr("#cross");
//update id of ifYes & ifNo
template.clone().find('#ifYes').attr("id", "#ifYes"+count);
template.clone().find('#ifNo').attr("id", "#ifNo"+count);
//loop through each input
var inputSection = template.clone().find(':input').each(function(){
//set id to store the updated section number
var newId = this.id + count;
//update id
this.id = newId;
}).end()
//inject new section with animation
.appendTo('#content').hide()
.appendTo('#content').slideDown(500)
return false;
});
//remove section
$('#content').on('click', '.cross', function() {
//slide up section
$(this).parent().slideUp(500, function(){
//remove parent element (main section)
$(this).parent().child().empty();
return false;
});
return false;
});
Appreciate your assistance.