I have the following div. That need to be added whenever i click on the button/link
<div class="add_dependent" style="display: none;">
<div class="dependent_content">
<span>
<strong>Dependent<span class="divcount"></span></strong>
</span><span class="delete">Delete</span>
<div><strong>Relationship to you</strong></div>
<div>
<input type="radio" value="0" name="relationship" class="dependent-relation" />
<label>Partner</label>
<input type="radio" value="1" name="relationship" class="dependent-relation"/>
<label>Child under21</label>
<input type="radio" value="2" name="relationship" class="dependent-relation"/>
<label>dependent over21</label>
</div>
<div><strong>Date of birth</strong></div>
<div>
<input type="text" id="dependent-dob" name="dependent-dob" datepicker/>
<input type="hidden" id="dependent-id" name="dependent-id" />
</div>
</div>
</div>
<a href="" id="add" ><img src="../../Content/themes/base/images/addnew_depnt.png" style="border: 0px;" alt="add dependent"></a>
Initially this div will be hidden. This div has to be added dynamically every time i click on link (Add dependent), and the div id of parent div should be assigned with dependent+id(1,2,3...).
I have used following script to work this, but the data entered in dependent1 is cloned in subsequent divs.
$("body").delegate("#add", "click", function () {
var total_length = $(".add_dependent").length
$(".add_dependent:eq(0)").clone().prependTo("#dependent-details").css("display", "inline");
$(".add_dependent:last-child .divcount").append(total_length)
$(".divcount").each(function (index) {
var increment = index + 1
$(this).parents(".add_dependent").attr("id", "depend" + increment)
$(this).text(increment)
});
})
How to add the above html elements to existing page whenever i click on the add dependent link?
Could anybody help me on this???