title: How to Validate checkbox before displaying popup in javascript
body: <p>I need to validate the checkbox checked or not before showing the popup static text</p>
  <p>This HTML code contains the questions and answers that I give as static in JavaScript and contains the popup style code.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function validate() {
    var checkboxes = document.getElementsByName("answer1");
    var checkboxChecked = [];
  
  
    for (var i = 0; i &lt; checkboxes.length; i++) {
  
    }
  }
  
  $(document).ready(function() {
    $('#trigger').click(function() {
  
      const question1answer1 = document.querySelectorAll('input[name="answer1"]');
      for (const question1answer1ans of question1answer1) {
  
        if (question1answer1ans.checked) {
          document.getElementById('question1-answer-1').innerHTML = question1answer1ans.value;
  
        }
      }
  
      const question1answer2 = document.querySelectorAll('input[name="answer2"]');
      for (const question1answer2ans of question1answer2) {
        if (question1answer2ans.checked) {
          document.getElementById('question1-answer-2').innerHTML = question1answer2ans.value;
        }
      }
  
      const question1answer3 = document.querySelectorAll('input[name="answer3"]');
      for (const question1answer3ans of question1answer3) {
        if (question1answer3ans.checked) {
          document.getElementById('question1-answer-3').innerHTML = question1answer3ans.value;
        }
      }
      const question1answer4 = document.querySelectorAll('input[name="answer4"]');
      for (const question1answer4ans of question1answer4) {
        if (question1answer4ans.checked) {
          document.getElementById('question1-answer-4').innerHTML = question1answer4ans.value;
        }
      }
  
      const question1answer5 = document.querySelectorAll('input[name="answer5"]');
      for (const question1answer5ans of question1answer5) {
        if (question1answer5ans.checked) {
          document.getElementById('question1-answer-5').innerHTML = question1answer5ans.value;
        }
      }
  
      const question1answer6 = document.querySelectorAll('input[name="answer6"]');
      for (const question1answer6ans of question1answer6) {
        if (question1answer6ans.checked) {
          document.getElementById('question1-answer-6').innerHTML = question1answer6ans.value;
        }
      }
  
      $('#question1overlay').fadeIn(300);
    });
    $('#question1close').click(function() {
      $('#question1overlay').fadeOut(300);
    });
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#question1popup {
    max-width: 600px;
    width: 90%;
    max-height: 600px;
    height: 100%;
    padding: 20px;
    position: relative;
    background: white;
    color: green;
    margin: 20px;
    margin-left: 35em;
  }
  
  #question1popups {
    max-width: 700px;
    width: 90%;
    max-height: 600px;
    height: 82%;
    padding: 20px;
    position: relative;
    background: black;
    color: green;
    margin: 20px auto;
  }
  
  #question1overlay {
    position: fixed;
    height: 100%;
    width: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    display: none;
  }
  
  #question1overlays {
    position: fixed;
    height: 100%;
    width: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    display: none;
  }
  
  #question1close {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
    color: #fbfbfb;
    background-color: black;
    padding: 10px;
    border-radius: 20px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
  
    &lt;!-- Activity needed packages --&gt;
    &lt;link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css"&gt;
    &lt;link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"&gt;
    &lt;script src="https://code.jquery.com/jquery-3.5.1.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  
  
    &lt;fieldset id="question1"&gt;
      &lt;legend&gt;Pick the fruits from the following&lt;/legend&gt;
      &lt;br&gt;
  
      &lt;div class="row"&gt;
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer1" VALUE="banana"&gt;&lt;br&gt;banana&lt;/label&gt;
        &lt;/div&gt;
  
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer2" VALUE="tomato"&gt;&lt;br&gt;tomato&lt;/label&gt;
        &lt;/div&gt;
  
  
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer3" VALUE="carrot"&gt;&lt;br&gt;carrot&lt;/label&gt;
        &lt;/div&gt;
  
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer4" VALUE="mango"&gt;&lt;br&gt;mango&lt;/label&gt;
        &lt;/div&gt;
  
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer5" VALUE="onion"&gt;&lt;br&gt;onion&lt;/label&gt;
        &lt;/div&gt;
  
        &lt;div class="col-md-2"&gt;
          &lt;label&gt;&lt;INPUT TYPE="checkbox" NAME="answer6" VALUE="apple"&gt;&lt;br&gt;apple&lt;/label&gt;
        &lt;/div&gt;
  
      &lt;/div&gt;
  
    &lt;/fieldset&gt;
  
  
  
  
    &lt;center&gt;&lt;button id="trigger"&gt;Check answer&lt;/button&gt;&lt;/center&gt;
    &lt;br&gt;
    &lt;br&gt;
    &lt;div id="question1overlay"&gt;
      &lt;div id="question1popup"&gt;
        &lt;div id="question1close"&gt;&lt;i class="fas fa-times-circle"&gt;&lt;/i&gt;&lt;/div&gt;
        &lt;h2 style="color: #4aa0dd;text-decoration-line: underline;"&gt;These are the Correct answers&lt;/h2&gt;
        &lt;br&gt;
        &lt;p style="color: green;"&gt;&lt;b&gt;&lt;i class="fas fa-arrow-circle-right"&gt;&lt;/i&gt;&amp;nbsp&amp;nbspOption 1&lt;/b&gt;&lt;/p&gt;
        &lt;p style="color: green;"&gt;&lt;b&gt;&lt;i class="fas fa-arrow-circle-right"&gt;&lt;/i&gt;&amp;nbsp&amp;nbspOption 4&lt;/b&gt;&lt;/p&gt;
        &lt;p style="color: green;"&gt;&lt;b&gt;&lt;i class="fas fa-arrow-circle-right"&gt;&lt;/i&gt;&amp;nbsp&amp;nbspOption 6&lt;/b&gt;&lt;/p&gt;
  
  
        &lt;h2 style="color: #4aa0dd;text-decoration-line: underline;"&gt;Your Answers&lt;/h2&gt;
        &lt;p id="question1-answer-1" style="color: green;font-weight: bold;"&gt;&lt;/p&gt;
        &lt;p id="question1-answer-2" style="color: red;font-weight: bold;"&gt;&lt;/p&gt;
        &lt;p id="question1-answer-3" style="color: red;font-weight: bold;"&gt;&lt;/p&gt;
        &lt;p id="question1-answer-4" style="color: green;font-weight: bold;"&gt;&lt;/p&gt;
        &lt;p id="question1-answer-5" style="color: red;font-weight: bold;"&gt;&lt;/p&gt;
        &lt;p id="question1-answer-6" style="color: green;font-weight: bold;"&gt;&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;br&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>The javascript code given in static text and more script is written manually into the HTML code
  instead given in particular js file script</p>