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

183
Vistas
How to specify an input id combining a literal and a for loop counter using js?

I have several js for "loops" and I need the cleanest syntax to specity an id value using the "for loop counter". Here is an example that works, but it looks kludgy and it doesn't thrill me much to have to use such janky code. See that:

"USCF_ID' +i +'"' +'

// USCF ID #

items += '<td style="display:none;">
<input onblur="showID();" name = "ID" id = "USCF_ID' +i +'"' +' name = "USCF_ID" /> </td>';

Can you make a suggestion about expressing ID = USCF_ID1 programically in plain js? No jQuery if you can avoid it, please.

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

0

It was kind of difficult for me to follow along with your question, but what I got from your explanation is that you're just trying to find a cleaner way to concatenate your data. I would recommend that you use string templates here. Here's an example:

items += `<td style="display:none;">
<input onblur="showID();" name = "ID" id = "USCF_ID${i}" name = "USCF_ID" /> </td>`;

Creating a string with the `...` syntax creates a string template literal that allows you to insert JavaScript code between the ${ } curly brackets and concatenate the result.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a string template where "USCF_ID' +i +'"' +' can be replaced by ${id}. String templates are defined using a backtick character - `:

items += `
  <td style="display:none;">
    <input onblur="showID();" name = "ID" id = "USCF_ID${id}" name = "USCF_ID" />
  </td>`;

Additionally you can extract the template into a function:

function getItem(id) {
  return `
  <td style="display:none;">
    <input onblur="showID();" name = "ID" id = "USCF_ID${id}" name = "USCF_ID" />
  </td>`;
}

Then you can generate your list as:

items += getItem(id);
about 4 years ago · Juan Pablo Isaza Denunciar

0

JS itself doesn't have built-in string formatting like other languages (with an exception later). You can simulate it yourself in a simple case like this with the replace method:

template = '<td style="display:none;"><input onblur="showID();" name="ID" id="USCF_ID$i" name="USCF_ID"/></td>';
...
items += template.replace("$i", i);

ES6 has template strings that can do this, but may not be available depending on the browser you are trying to support (note the backticks instead of single quotes):

items += `<td style="display:none;"><input onblur="showID();" name="ID" id="USCF_ID${i}" name="USCF_ID"/></td>`;
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