I’m having problem to select my option when the div has been cloned.
Once cloned I want to be able to select the options and give a selected class. So each cloned div has e selected option.
Please check my demo and try to clone.
Jquery
var cloneCount = 0;
$("#add-address").click(function() {
$("#to-add-address").clone()
.attr("id", "to-add-address_Clone" + cloneCount)
.insertAfter("#to-add-address");
$("#clone", "#to-add-address_Clone" + cloneCount)
.attr("id", "clone_Clone" + cloneCount);
cloneCount++;
$('.options li a').bind('click',function () {
$('.options li a').removeClass('selected');
$(this).addClass('selected');
});
});
HTML
<ul>
<li id="to-add-address" class="outerDiv address" >
<div id="clone">
<label><input type="text" value="Address"><span class="input-edit"></span></label>
<label><input type="text" value="Address 2"><span class="input-edit"></span></label>
<label><input type="text" value="Town"><span class="input-edit"></span></label>
<label><input type="text" value="Contry"><span class="input-edit"></span></label>
<label><input type="text" value="Post Code"><span class="input-edit"></span></label>
<ul class="options">
<li class="home"><a href="javascript:void(0);">home</a></li>
<li class="work"><a href="javascript:void(0);">work</a></li>
<li class="other"><a href="javascript:void(0);">other</a></li>
<li class="delete"><a href="javascript:void(0);">delete</a></li>
</ul>
</div>
</li>
</ul>
<a href="javascript:void(0);" id="add-address">clone</a>
You can change it to this to only affect the elements within that clone:
Additionally, you are re-binding click each time causing the number of listeners on each element to increase with every clone, instead use event delegation:
if your real code has a common parent, replace
documentwith the appropriate selector.http://jsfiddle.net/XeELs/61/