I am using a <a> on click of which i am cloning a div. In div i have two radio buttons with same name.
Now the problem is when i select one radio option and clone the div the next two radio buttons come with same name and selected radio button from last div got unselected.
$('.addMore').on('click', function() {
$('.addMoreDiv:last').clone().insertAfter('.addMoreDiv:last');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="addMoreDiv">
<label>
<input type="radio" name="optradio">Radio 1
</label>
<label>
<input type="radio" name="optradio">Radio 2
</label>
</div>
<a href="#" class="addMore">Add More</a>
I tried updating name according to length but didn't work and name updates after addition of div.
Is there any option so that when i add new div it comes with different names also the selection of last div will not effect?
clone()method just add one more copy of the element. The element's attributes remain the exact same. But you are not changing thenameattribute in your code .var, edit the name of it, and then reinsert it?