This might sound very strange and probably it is, so forgive me if I'm committing a bad mistake.
I have an unspecified number of <input type='text'>, generated at runtime and identified with a progress number id like id='data1', id='data2'...I would like to have a jQuery script on every input. I thought to generate x scripts for x inputs but it doesn't work.
How could I do this?
function addPreparations (nos, pietanze) {
var numeroTotale = nos.value;
var box = document.getElementById("box_righe");
generateScripts(numeroTotale);
if (numeroTotale == '') {
box.innerHTML = '';
}
else {
var righe = "<table class='table table-hover'>";
righe += "<th class='text-center'>Pietanza</th><th class='text-center'>U. di Misura</th><th class='text-center'>Quantità</th><th class='text-center'>Cuoco</th><th class='text-center'>Data di Preparazione</th>";
for (i = 1; i <= numeroTotale; i++) {
righe = righe + "<tr><td><select name='pietanza"+i+"' class='form-control' onchange='showMU(this.value, \"p\", "+i+");'>";
righe = righe + "<option value=''>Selezionare la pietanza "+i+"</option>";
for (j=0; j<pietanze.length; j++) {
righe = righe + "<option value='"+pietanze[j]+"'>"+pietanze[j]+"</option>";
}
righe = righe + "</select></td>";
righe = righe + "<td align='center'><p id='umis"+i+"' class='h5'>- - -</p></td>";
righe = righe + "<td><input type='number' placeholder='Inserire la quantità' name='quantita"+i+"' class='form-control'/></td>";
righe = righe + "<td><input type='text' placeholder='Inserire il cuoco' name='cuoco"+i+"' class='form-control'/></td>";
righe = righe + "<td><input type='text' placeholder='GGMMAAAA' id='data"+i+"' class='form-control' name='data"+i+"' /></td></tr>";
}
righe = righe + "</table>";
righe = righe + "<input type='submit' name='storageConfirm' value='Conferma' class='btn btn-success'/>  ";
righe = righe + "<input type='reset' value='Reimposta' class='btn btn-danger'/>";
box.innerHTML = righe;
}
}
function generateScripts (total) {
for (i=1; i<=total; i++) {
$(document).ready(function() {
$("body").on('keydown', '#data'+i, function(e) {
if ((e.keyCode == 8) || (e.keyCode >= 96 && e.keyCode <= 105) || (e.keyCode >= 48 && e.keyCode <= 57)) {
return;
}
else {
alert("Solo numeri!");
e.preventDefault();
}
});
});
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>