Assign value to the dynamic jQuery element

1

I have the following for where I try to fill a dynamic input text, the question is that I do not know how to assign the path value of for to be able to fill them correctly:

for (var j = 0; j < respuesta.length; j++) {
         //console.log("indice", i);
          $("caja" + j + ).val(bolsas2[j]); //Obviamente no funciona, necesito asignar en el input text id=caja"x" el valor "x"
 }

Thank you very much for the help.

EDITED

I create the inputs with the first for, with the second the full ones

$(".box-body").html("");
for (var k = 0; k < numCa; k++) {
     console.log("indice", k);
     $(".box-body").append('<div class="form-group linea"><div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i></span><input type="text" class="form-control input-lg" id="caja ' + +'" name="caja ' + k + '" value="123" required></div></div>');
}
for (var j = 0; j < respuesta.length; j++) {
     //console.log("indice", i);
      //$("#caja " + j).val(bolsas2[j]);
      console.log($("#caja " + j).val());
      console.log("Bolsas: ", bolsas2[j]);
      console.log($("#cajas " + j).find('input[type="text"].form-control').attr('id'));
}

I can not see the console value of "123" using this line console.log($("#caja " + j).val()); , I feel it is not the correct way to catch the element

    
asked by Baker1562 19.02.2018 в 22:09
source

2 answers

1

It's just a matter of concatenating the value of the iterating variable of the for cycle with + (I was guided by previous responses to generate the inputs)

for (var i = 0; i < 10; i++) {
  $(".box-body").append('<div class="form-group linea"><div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i></span><input type="text" class="form-control input-lg" id="caja ' + i + '" name="caja ' + i + '" value="' + i+'" required></div></div>');
}
.linea{
  background : #ccc;
  width: 200px;
  height: 50px;
  margin :2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box-body">
  
</div>

If you have already created the boxes with a id it would be a matter of iterating and assigning the value of j depending on the case of the index.

for (var j = 0; j < respuesta.length; j++) {
  $("#caja" + j ).val(j); // Para asignar el indice del for
  // Para añadir el valor en el indice j del array bolsas2
  $("#caja" + j ).val(bolsas2[j]); 
}

If the inputs are created as in your previous question , it has an error of spaces when assingnar% id , id="caja ' + i + '"
it should be without the space id="caja' + i + '"

    
answered by 19.02.2018 / 22:12
source
2

Your only mistake was that you had a + left in your selector and you still had to add the element's indicative either a # to indicate that you want to select by means of the attribute id or a . to indicate that you want to select using the attribute class

var bolsas2 = ['valor 1', 'valor 2', 'valor 3']

for (var j = 0; j < 3; j++) {
    $("#caja" + j).val(bolsas2[j]);
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="caja0" type="text">
<input id="caja1" type="text">
<input id="caja2" type="text">
    
answered by 19.02.2018 в 22:18