I’m busy with a form that needs to have an option to duplicate certain fields as many times as the user click, then also it needs to be E-Mailed off, therefore I’m currently on the passport field.
The only problem is, the way i set it up is that for each div element with the class off “passport” it creates an input field. So now where the fields ids are:
1 2 3 4 5 6 7 8 9 10 11 12 13
and when i use jQuery clone:
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13
and what i need it to be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
but then how do i assign only the first set of ids to be associated with that one input, for it evaluates the value of the fields.
Anyways, Any help/advice greatly Appreciated.
Heres the jsfiddle: http://jsfiddle.net/dawidvdh/A5naZ/
Heres the code:
jQuery:
$(document).ready(function() {
var pass = 1;
val = 0;
jQuery('div.passport').each(function() {
var index = "pass"+pass++;
var passer = "<input class='pass' id="+'"'+index+'"'+" "+" maxlength='1' />";
var passvalue = $(this).attr('value');
jQuery(passer).appendTo('#pass');
});
$("#clone").click(function () {
$('#pass').clone().insertAfter("#pass");
});
function Validatepass() {
var passValue = '';
$('.pass').each(function(){ passValue+=($(this).val());});
jQuery('#error p').remove();
var error = jQuery('#error');
var passNumber = passValue;
var correct = true;
if (passNumber.length != 13) {
correct = false;
}
// if no error found, hide the error message
if (correct) {
jQuery('.pass').css("border","1px solid #9A8B7D");
}
// otherwise, show the error
else {
jQuery('.pass').css("border","1px solid #FF0000");
}
return false;
}
$('input.pass').keydown(function(e){
if(e.keyCode == 8){
$(this).val('');
$(this).prev().val('');
$(this).prev().focus();
Validatepass();
}
});
$('input.pass').on('keyup', function(){
if (this.value.match(/\d+/)) {
var $this = $(this);
if ($this.next('input').length) {
$this.next().focus();
} else {
Validatepass();
}
}
});
});
and the HTML:
<div id="pass"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<div class="passport"></div>
<button id="clone">duplicate</button>
change the click function to this
EDIT: check this jsfiddle
EDIT 2: As per your second comment, if u mean changing the
idof the cloneddiv, Added in the code.