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

123
Vistas
Make Function with querySelectAll with forEach

I want to create a javascript querySelectAll with foreach. I need to use this many times. So i want to create a function. But not working. Please help me.

<p id="o">one</p>
<p id="o">two</p>

<script>
function q(x,y) {
document.querySelectorAll(x).forEach(e => { e.y })
}
    
q('#o', innerHTML = 'Working');
</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to put some code in a reusable form that you can pass around then it needs to be a function.

An expression is just going to be evaluated immediately.

innerHTML = 'Working' will assigned the string to the global variable innerHTML and then the pass the result (the string) to y.

You need it to be a function and then you need to call it.

What's more: An ID must be unique in a document. If you want to loop over a group of things then use a class.

function q(x, y) {
  document.querySelectorAll(x).forEach(y)
}

q('.o', (element) => element.innerHTML = 'Working');
<p class="o">one</p>
<p class="o">two</p>

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