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

198
Vistas
How to add dynamic id to text boxes and fetch its value?

I'm trying to fetch value of each textbox keyup event inside for loop.And after the series of text boxes are created, I want to call keyup event, which fetch the value entered in each text box

what i have tried

script for assigning dynamic id

<textarea type="text" name="add'+ $i +'" id="add'+ $i +'"
value="" class="form-control" onkeyup="callans()" ></textarea> <br/>

function callans() {
for( var i=0; i<8; i++) 
   //i<9 because that's the maximum number of text 
  //fields to be created is 8.     
    {        




<textarea type="text" name="add'+ $i +'" id="add'+ $i +'"
value="" class="form-control" onkeyup="callans()" ></textarea> 

  var elements = document.getElementById("add'+ $i +'").value;

       alert(elements); 


    }
}

Is there a way only create the id's dynamically and append it in the input area?

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

0

Without using ID attributes you can very easily identify elements in the DOM by inspecting the event - in your case you are interested in the keyup event and to make life easier by using a delegated event listener bound to the input elements common parent we can obtain the values like so:

(vanilla js mainly )

for( var i=0; i<8; i++){        
    $('#addition').append('<input class="form-control" type="text" name="add" />');
}

// delegated event listener bound to the parent container but processing only `INPUT` elements of type `text`
document.querySelector('#addition').addEventListener('keyup',function(e){
  if( e.target.tagName=='INPUT' && e.target.type=='text' ){
    console.log(e.target.value)
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id='addition'></div>

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