i have a form with various fields, and a pair of buttons of clone and remove the cloned part of the form.
Also, i have in those fields a pair of inputs of integers that when i click in one of them the value "jump" to the other field when clicked.
The problem is when i clone the form the functionality of "jump" do not attach to the other cloned inputs.
this is the clone function :
var regex = /^(.+?)(\d+)$/i;
var cloneIndex = 1;//$(".clonedInput").length;
function clone(){
cloneIndex++;
$(this).parents(".clonedInput").clone()
.appendTo("form")
.attr("id", "clonedInput" + cloneIndex)
.find("*")
.each(function() {
var id = this.id || "";
var match = id.match(regex) || [];
if (match.length == 3) {
this.id = match[1] + (cloneIndex);
//console.log(this.val);
//this.value = $(match).val();
//console.log("El valor seleccionado es ");
//this.val = match[1].val;
}
})
.on('click', 'button.clone', clone)
.on('click', 'button.remove', remove);
return false;
}
function remove(){
if($('.actions').length == 2){
console.log('accion cancelada');
}else{
$(this).parents(".clonedInput").remove();
}
return false;
}
$("button.clone").on("click", clone);
$("button.remove").on("click", remove);
an approach to this was made with this code using dinamicaly for php
$("input[id^='montoa']").on("click", function(e){
var montoa_id = this.id;
var montob_id = 'montob'+montoa_id.match(/(\d+)/g)[0];
$('#'+montoa_id).value = $('#'+montob_id).val();
$('#'+montob_id).value = '';
});
the inputs are this :
<div class="col-md-1">
<input type="text" name="monto[]" class="form-control" id="montoa1" placeholder="Debe">
</div>
<div class="col-md-1">
<input type="text" name="monto[]" class="form-control" id="montob1" placeholder="Haber">
</div>
and all the n-cloned fields are numbered by auto-increase the id like id="montoa2"
and id="montob3"
and so on.
all comments wil be very appreciated.
EDIT : Create a jsfiddle https://jsfiddle.net/o63c61sj/