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

196
Visualizações
Can I iterate +1 through a javascript query selector?

So I have some radio buttons and I am currently checking to see if they are checked and am getting the value from it find using this:

var selectedAnswer = document.querySelector('input[name="question1"]:checked').value;

This returns what I want it to but I have several different forms, each with the name question1, question2, and so forth. Is there any way I can use that querySelectory method and have name="question" + 1 each time, such that I can loop through each question in every form. If I can find out if this is legal or not I can easily accomplish it with a for loop.

I have tried this:

for (let i = 1; i <= 5; i++) {
    var selectedAnswers = document.querySelector(`input[name="question${i + 1}]:checked`).value            
}

Yet it the the value is returning null, meaning the interpolation is failing.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just go for querySelectorAll() and a forEach() loop:

let questions = document.querySelectorAll('.questions')

questions.forEach((e)=>{
    e.addEventListener('change',function(){
        console.log(e.value);//modify here according to your need
    })
})
<input type="radio" name="question" value="1" class="questions">
<label>Q1</label>
<br>
<input type="radio" name="question" value="2" class="questions">
<label>Q2</label>
<br>
<input type="radio" name="question" value="3" class="questions">
<label>Q3</label>
<br>
<input type="radio" name="question" value="4" class="questions">
<label>Q4</label>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a solution, where no loops are there.

function handleChange(thisCheckbox) {
  console.log(thisCheckbox.value);
}
<input type="radio" name="question" value="1" class="questions" onchange="handleChange(this)">
<label>Q1</label>
<br>
<input type="radio" name="question" value="2" class="questions" onchange="handleChange(this)">
<label>Q2</label>
<br>
<input type="radio" name="question" value="3" class="questions" onchange="handleChange(this)">
<label>Q3</label>
<br>
<input type="radio" name="question" value="4" class="questions" onchange="handleChange(this)">
<label>Q4</label>

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