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

98
Visualizações
How to disable a group of radio buttons if one has been selected and display an alert if one is not selected and the next button is clicked

I am trying to disable the rest of the radio buttons if one has been selected for a multiple-choice quiz. I tried using a forEach loop but it is skipping the forEach loop and just triggering the alert box in the else part of the if statement when I click a radio button before one is even selected. Here is the function I am working in. the name${questionNumber} is the way to call my radio buttons. The myQuestions is my object with all of the questions and answers stored in it. Can someone please help me figure out how to do this, I would appreciate it. Thanks.

function showExplanation(questionNumber) {
  document.getElementsByClassName("explanations")[questionNumber].classList.add("show");

  if (document.getElementsByName(`name${questionNumber}`).checked) {
    myQuestions.forEach((questionNumber) => {
      document.getElementsByName(`name${questionNumber}`).disabled = true;
    });
  } else {
    alert("Please select an answer.");
  }
}
const myQuestions = [{
    question: "What color is the sky?",
    answers: {
      a: "blue",
      b: "green",
      c: "red",
      d: "yellow"
    },
    explanation: {
      correct: "Correct answer: A. blue",
      explain: `The sky may be blue today
          `,
      source: "Source: common sense"
    },
    correctAnswer: "a"
  },
  {
    question: `What color is an elephant?`,
    answers: {
      a: "blue",
      b: "grey",
      c: "black",
      d: "green"
    },
    explanation: {
      correct: "Correct answer: B. grey",
      explain: `Most elephants are grey
        `,
      source: "Source: life experience"
    },
    correctAnswer: "b"
  }
];
<!--    START OF QUIZ-->
<div id="border" class="uk-align-center uk-width-1-2">
  <div class="quiz-body">
    <h1 class="h1-quiz">Test Your Knowledge</h1>
    <div class="quiz-container">
      <div id="quiz"></div>
    </div>
    <div id="results" class="uk-text-center uk-text-large uk-text-bold uk-margin-small-top"></div>

  </div>

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

0

You mean When Correct User Can Only Choose one answer and when User select one answer other's will be disabled ??

What is your HTML code look like ??

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