A couple of unexpected bugs here that I can't figure out.
- Clicking the '+' button works all the time except when you '-' all the cloned divs away until there is one left.. then the '+' button no longer works.
- When the div is cloned, it is supposed to clear the checkboxes and radio buttons.. but it doesn't. It DOES clear the text fields and the select.
General code help always welcome! Thanks...
<div class="cloned" id="div1">
<input type="text" id="_name" name="1_name" placeholder="Field Name" required>
<input type="text" id="_hint" name="1_hint" placeholder="Hint">
<select id="_fieldtype" name="1_fieldtype" required>
<option value="">Field Type...</option>
<option value="bla">bla</option>
<option value="blabla">blabla</option>
</select>
<input type="checkbox" id="_required" name="1_required" value="true"> Required
<input type="checkbox" id="_search" name="1_search" value="true"> Searchable
<input type="checkbox" id="_editable" name="1_editable" value="true"> Editable
<input type="radio" id="_label" name="label" value="1_label"> Label
<input type="radio" id="_unique" name="unique" value="1_unique"> Unique
<input type="button" class="add" value="+">
<input type="button" class="remove" value="-">
</div>
<script>
function addDiv(){
window.num = $('.cloned').length;
var num = $('.cloned').length;
var newnum = num + 1;
var newelem = $('#div'+num).clone().attr('id','div'+newnum);
$.each(newelem.children(), function(){
if (this.type == 'radio'){
$(this).attr('value',newnum+this.id).removeAttr('checked')
}
else if (this.type == 'button'){
$(this).removeAttr('checked')
}
else if (this.type != 'button'){
$(this).attr('name',newnum+this.id).attr('value','')
}
});
$('#div'+num).after(newelem);
};
function removeDiv(object){
window.num = $('.cloned').length;
if (num != 1)
$(object.parentNode).remove();
};
$('.add').live('click', function(){
addDiv();
});
$('.remove').live('click', function(){
removeDiv(this);
});
</script>