Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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?

Studi

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


//
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?
 

Buy us a coffee!

Back
Top Bottom