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

176
Visualizações
¿Cómo selecciono el siguiente elemento en una matriz cuando un usuario hace clic en un botón?

Estoy haciendo una prueba usando 4 botones para responder las preguntas (tengo 6 preguntas en total).

¿Cómo obtengo el siguiente conjunto de preguntas de mi matriz? Se parece a esto:

 var questions = [{ question: "the question!". answer1: "choice1", answer2: "choice2", answer3: "choice3", answer4: "choice4", correct: "choice3", }}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Simplemente realice un seguimiento del índice actual en la matriz y, cuando hagan clic, incremente el índice. Y luego solo tendría un método de render que toma el índice como parámetro. Algo como:

 const questions = [{ question: "the question!", answer1: "choice1", answer2: "choice2", answer3: "choice3", answer4: "choice4", correct: "choice3" }, { question: "the next question!", answer1: "A", answer2: "B", answer3: "C", answer4: "D", correct: "C" }]; var currentQuestionIndex = 0; const c = document.getElementById('content'); const render = (idx) => { const q = questions[idx]; c.innerHTML = `<strong>${q.question}</strong> <p><label><input name="q" type="radio"/>${q.answer1}</label> <p><label><input name="q" type="radio"/>${q.answer2}</label> <p><label><input name="q" type="radio"/>${q.answer3}</label> <p><label><input name="q" type="radio"/>${q.answer4}</label> `; } render(currentQuestionIndex); document.getElementById('btn').addEventListener('click', () => { currentQuestionIndex = (currentQuestionIndex + 1) % questions.length; render(currentQuestionIndex); });
 <div id="content"></div> <button id="btn">Next</button>

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