Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
¿Cómo puedo salir del bucle en Javascript?

Quiero que cuando el usuario haga clic en una opción del cuestionario no pueda volver a hacer clic en ella. Para eso, necesito salir de "for loop" (al final de este código), pero cuando escribo una declaración de interrupción, de alguna manera es incorrecta. ¿Podrías decirme la forma correcta de hacerlo? Agradecería mucho la respuesta! :)

 <script> document.addEventListener("DOMContentLoaded", function() { function clicked(answer, i) { if (answer[i].value == "correct") { answer[i].style.backgroundColor = "green"; let result = "<p>Correct!</p>"; document.getElementsByClassName("section")[0].insertAdjacentHTML('beforeend', result); return; } else { answer[i].style.backgroundColor = "red"; let result = "<p>Wrong!</p>"; document.getElementsByClassName("section")[0].insertAdjacentHTML('beforeend', result); return; } } let answer = document.getElementsByTagName('button'); for (let i = 0; i < answer.length; i++) { answer[i].addEventListener("click", () => { { clicked(answer, i); } }); } }); </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tu logica esta mal...

Su ciclo for está configurando el evento de clic en todas sus respuestas, romperlo solo significaría que no agregaría el evento en todas las respuestas.

Lo que tienes que hacer es:

  • elimine el evento de clic en todas las respuestas DESPUÉS del primer clic (en su función clicked())
  • establezca un indicador verdadero/falso "hasBeenClickedOn" que verificará al comienzo de la función clicked()

Por ejemplo :

 <script> var hasBeenClickedOn = false; // global flag document.addEventListener("DOMContentLoaded", function() { function clicked(answer, i) { if (hasBeenClickedOn===true) { return false; } // check the flag hasBeenClickedOn = true; // set the flag if (answer[i].value == "correct") { answer[i].style.backgroundColor = "green"; let result = "<p>Correct!</p>"; document.getElementsByClassName("section")[0].insertAdjacentHTML('beforeend', result); return; } else { answer[i].style.backgroundColor = "red"; let result = "<p>Wrong!</p>"; document.getElementsByClassName("section")[0].insertAdjacentHTML('beforeend', result); return; } } let answer = document.getElementsByTagName('button'); for (let i = 0; i < answer.length; i++) { answer[i].addEventListener("click", () => { { clicked(answer, i); } }); } }); </script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!