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

105
Vistas
How to Loop Through Elements Ending with a number

I have a form with five input statements with the id ending with a sequential number like:

<input id="phone1" type="text" value="1111111111">
<input id="phone2" type="text" value="">
<input id="phone3" type="text" value="3333333333">
<input id="phone4" type="text" value="4444444444">
<input id="phone5" type="text" value="">

I attempted the following:

const nmbr = [1, 2, 3, 4, 5];
$.each(nmbr, function(index, value){
  var phn = $("#phone"+value)
  if !(phn.val()) {
     console.log('phone number missing');
   }
});

How would I step through these id's to get the value of the non-blank phone numbers.

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

0

You can use a class instead and then loop through the input and check if value is non empty

const inputs = document.querySelectorAll('.ipt');

for (const ipt of inputs) {
  if (ipt.value !== "") {
    console.log(ipt.value);
  }
}
<input class="ipt" id="phone1" type="text" value="1111111111">
<input class="ipt" id="phone2" type="text" value="">
<input class="ipt" id="phone3" type="text" value="3333333333">
<input class="ipt" id="phone4" type="text" value="4444444444">
<input class="ipt" id="phone5" type="text" value="">

about 4 years ago · Juan Pablo Isaza Denunciar

0

In Below Code document.querySelectorAll('[id^="phone"]') will select all the ids with phone prefix.

    const data =  document.querySelectorAll('[id^="phone"]');
    console.log(data);
    
    
    for (const dt of data) {
      if (dt.value !== "") {
        console.log(dt.value);
      }
    }
    <input id="phone1" type="text" value="1111111111">
    <input id="phone2" type="text" value="">
    <input id="phone3" type="text" value="3333333333">
    <input id="phone4" type="text" value="4444444444">
    <input id="phone5" type="text" value="">

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