mardi 25 mai 2021

Checking to see if a checkbox is selected before searching

I am having some trouble with my checkboxes. I am trying to get a returned true or false value out of event listeners on checkboxes and then passing it through an event listener on a button to confirm that at least one checkbox is selected. I'm sorry if this sounds confusing and I feel like there is an easier way to do this. I would like to solve this with pure JavaScript. Really appreciate the help. Below is the code.

<body>
    <script src="racquetObjects.js"></script>
    <div class="mainContainer">
      <div class="selectors">
        <form action="">
          <div class="checkboxes">
            <input type="checkbox" id="babolat" value="Babolat" />
            <label for="babolat">Babolat</label>
            <input type="checkbox" id="wilson" value="Wilson" />
            <label for="wilson">Wilson</label>
            <input type="checkbox" id="power" value="Power" />
            <label for="power">Power</label>
            <input type="checkbox" id="control" value="Control" />
            <label for="control">Control</label>
            <input type="checkbox" id="popular" value="Popular" />
            <label for="popular">Popular</label>
          </div>
          <button type="button" id="find">Find</button>
        </form>
      </div>
      <div class="racquetContainer"></div>
      <div class="bench"></div>
    </div>
    <script src="racquetFinderCB.js"></script>
    <script src="racquetFinder.js"></script>
  </body>

const findButton = document.querySelector("#find");
const checkboxes = document.querySelectorAll(
  ".checkboxes input[type=checkbox]"
);

const checkboxesAreChecked = checkboxes.forEach((el) => {
  el.addEventListener("click", (e) => {
    if (e.target.checked) {
      return true;
    } else {
      return false;
    }
  });
});

const beforeSubmit = () => {
  if (checkboxesAreChecked === true) {
    console.log("Time to search!");
  } else {`enter code here`
    console.log("You need to select an option");
  }
};



Aucun commentaire:

Enregistrer un commentaire