Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
How can I dynamically select input field in dynamically generated forms with Javascript/jQuery?

When my document is ready I have 1 or more identically forms with select and two inputs that I want to show (1 of them based on select dynamically).

For 1 form its pretty simple, but my problem is that forms are generated dynamically like when I have on ready 1 form select tag is looking like this

<button type="button" class="add-document-item btn btn-success btn-sm pull-right">
  <i class="glyphicon glyphicon-plus"></i>
</button>
<select id="type" class="form-control">
   <option value="1">File 1</option>
   <option value="2">File 2</option>
</select>

<div class="col-md-12 system" id="system">
   My YII2 active form field...                       
</div>
<div class="col-md-12 computer" id="computer">
   My YII2 active form field...                       
</div>

If after opening document and there is more than 1 form OR there is one and I adding more by myself dynamically, SELECT id's generating like "type0", "type1", "type2" and same is with form fields id's.

So far I can only switch inputs dynamically only when 1 form is rendered

$(document).ready(function () {
  $('#computer').hide();
  $(document).on('change', '#type', function () {
     var val = $('#type').val();
     if (val == 2) {
       $('#system').show();
       $('#computer').hide();
     } else {
       $('#computer').show();
       $('#system').hide();
     }
   });
 });

My need is to change selected input for every generated form individual. I am stuck for this already for some time. Can anybody can help me?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This javascript I created should do the trick. You'll need to apply this to your own code.

  1. Declare an array with all form elements

  2. Select all elements you are going to loop through

  3. Use forEach and overload it with the array you created

  4. Create an if statement with the right filter set

     myArray = document.querySelectorAll('form')
    
     document.querySelectorAll('form').forEach(function(value, index,    
     myArray){
    
     if (myArray[index].getAttribute('id')=='system'){
     console.log('system') //doSomething()
    
     }
     })
    

You can refactor this by replacing the 2th instance of document.querySelectorAll('form') with myArray, but for clarity I'll leave it as is.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda