i have a single filed which i need to clone with a clone button and remove button to remove the cloned filed only
i made this simple script but i believe it contain something wrong since its not working 🙂
HTML
<form method="post">
<div id="fileds">
<select name="lang" id='lang'>
<option>select language</option>
</select>
</div>
</form>
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
JS
$(function(){
var regex = /^(.*)(\d)+$/i;
var cloneIndex = $("#lang").length;
$("button.clone").live("click", function(){
$(this).parents("#lang").clone()
.appendTo(".fileds")
.attr("id", "lang" + cloneIndex)
.find("*").each(function() {
var id = this.id || "";
var match = id.match(regex) || [];
if (match.length == 3) {
this.id = match[1] + (cloneIndex);
}
});
cloneIndex++;
});
});
also i didn’t find how to write the remove code for the remove button
Thank you
1) The
#langis not a parent of.clone2)
.fieldsshould be#fieldas this is and IDThis code should work. Live Demo