• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to close the selection if I click outside?


New Coder
const selectBtn = document.querySelectorAll(".select-btn");
const options = document.querySelectorAll(".select-btn label");
selectBtn.forEach(function (button, index) {
  button.addEventListener("click", () => {
    selectBtn.forEach(function (button2, index2) {
      if (index !== index2) {
options.forEach((button) => {
  button.addEventListener("click", (e) => {
    const newLabel = e.target.innerText;
    const defaultLabel = e.target.parentElement.firstChild.nextElementSibling;
    defaultLabel.innerText = newLabel;

how or where can I add a function that will remove classList "active" from the selectBtn if I click anywhere on the document and can still add back to it if I click on the button?
Top Bottom