0

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'/>&nbsp&nbsp";
    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>

3
  • can you add a class to your inputs & use it as a selector? Commented Mar 15, 2017 at 15:04
  • I'll post my full code, so it will be more clear Commented Mar 15, 2017 at 15:04
  • in your jquery code, in the for loop, check if the '#data'+i exists, if not, exit the loop? Commented Mar 15, 2017 at 15:06

2 Answers 2

4

Use attribute selector [] with the start with ^ selector ([id^="data"]), your code should be :

$(document).ready(function() {
    $("body").on('keydown', '[id^="data"]', function(e) {
        //Your logic HERE
    });
});

Or you could give all your inputs a common class and use it as selector like :

$(document).ready(function() {
    $("body").on('keydown', '.common_class', function(e) {
        //Your logic HERE
    });
});

NOTE : It's better to use the input event when you track the user input, like :

$(document).ready(function() {
    $("body").on('input', '[id^="data"]', function(e) {
        //Your logic HERE
    });
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Why @Nomistake :) ?
@Zakaria dont remember 😀 maybe: he's talking about '... id like id='data1', id='data2'...' so my guess it has to be unique for an untold reason...
0

You do not need the loop here. What you want to do is add a class to each of these elements. Something like inputdata; make sure this same class is on all of the inputs.

Then you can just do:

$(document).ready(function(){
    $('body').on('click', '.inputdata', function(e){
    });
});

3 Comments

he asks for id's
@Nomistake: True, but adding a class is still a valid solution.
Maybe. But he's talking about '... id like id='data1', id='data2'...' so my guess it has to be unique for an untold reason...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.