Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda