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 hi guys, i want other accordion items close when one item collapses. in other words only one item be open.

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 50%;
      }
      .accordionBtn {
        background-color: grey;
        width: 100%;
        height: 60px;
        text-align: left;
      }
      .active,
      .accordionBtn:hover {
        background-color: lightslategray;
      }
      .panel {
        background-color: antiquewhite;
        display: none;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <button class="accordionBtn">Content 1</button>
      <div class="panel">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error
          temporibus quasi tempora doloribus, similique natus reprehenderit.
        </p>
      </div>
      <button class="accordionBtn">Content 2</button>
      <div class="panel">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error
          temporibus quasi tempora doloribus, similique natus reprehenderit.
        </p>
      </div>
      <button class="accordionBtn">Content 2</button>
      <div class="panel">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error
          temporibus quasi tempora doloribus, similique natus reprehenderit.
        </p>
      </div>
    </div>
    <script>
      let accordion = document.querySelectorAll(".accordionBtn");
      let i;
      for (i = 0; i < accordion.length; i++) {
        accordion[i].addEventListener("click", function () {
          this.classList.toggle("active");
          let panel = this.nextElementSibling;
          if (panel.style.display === "block") {
            panel.style.display = "none";
          } else {
            panel.style.display = "block";
          }
          /*if (panel.target.parentNode.classList.contains("active")) {
            panel.target.parentNode.classList.remove("active");
            return;
          } */
        });
      }
    </script>
  </body>
</html>
 
Last edited by a moderator:
Solution
Hey there.
So what you could do is hide all of the elements before making the one you want to open visible.
JavaScript:
const accordion = document.querySelectorAll(".accordionBtn");

for (let i = 0; i < accordion.length; i++) {
  accordion[i].addEventListener("click", function () {
    const panel = this.nextElementSibling;

    if (panel.style.display === "block") {
      this.classList.remove("active");
      panel.style.display = "none";
    } else {
      accordion.forEach(button => {
        button.clasList.remove("active");
        button.nextElementSibling.style.display = "none";
      });
      this.classList.add("active");
      panel.style.display = "block";
    }
  });
}

Or if you have some sort of animations, or for any other...
Hey there.
So what you could do is hide all of the elements before making the one you want to open visible.
JavaScript:
const accordion = document.querySelectorAll(".accordionBtn");

for (let i = 0; i < accordion.length; i++) {
  accordion[i].addEventListener("click", function () {
    const panel = this.nextElementSibling;

    if (panel.style.display === "block") {
      this.classList.remove("active");
      panel.style.display = "none";
    } else {
      accordion.forEach(button => {
        button.clasList.remove("active");
        button.nextElementSibling.style.display = "none";
      });
      this.classList.add("active");
      panel.style.display = "block";
    }
  });
}

Or if you have some sort of animations, or for any other reason, the above code is not possible, you could also do it this way; Select every of the buttons that wasn't clicked and hide them, while making the clicked button visible:
JavaScript:
const buttons = Array.from(document.querySelectorAll(".accordionBtn"));    // Select all accordion buttons and change data type to array

buttons.forEach(thisButton => {
  const otherButtons = buttons.filter(button => (button !== thisButton));  // Filter the clicked button out of the element array
  const panel = thisButton.nextElementSibling;

  thisButton.addEventListener("click" => {
    if (thisButton.classList.contains("active")) {
      thisButton.classList.remove("active");
      panel.style.display = "none";
    } else {
      thisButton.classList.add("active");
      panel.style.display = "block";
      otherButtons.forEach(button => {
        button.classList.remove("active");
        button.nextElementSibline.style.display = "none";
      });
    }
  });
});
 
Solution
Hey there.
So what you could do is hide all of the elements before making the one you want to open visible.
JavaScript:
const accordion = document.querySelectorAll(".accordionBtn");

for (let i = 0; i < accordion.length; i++) {
  accordion[i].addEventListener("click", function () {
    const panel = this.nextElementSibling;

    if (panel.style.display === "block") {
      this.classList.remove("active");
      panel.style.display = "none";
    } else {
      accordion.forEach(button => {
        button.clasList.remove("active");
        button.nextElementSibling.style.display = "none";
      });
      this.classList.add("active");
      panel.style.display = "block";
    }
  });
}

Or if you have some sort of animations, or for any other reason, the above code is not possible, you could also do it this way; Select every of the buttons that wasn't clicked and hide them, while making the clicked button visible:
JavaScript:
const buttons = Array.from(document.querySelectorAll(".accordionBtn"));    // Select all accordion buttons and change data type to array

buttons.forEach(thisButton => {
  const otherButtons = buttons.filter(button => (button !== thisButton));  // Filter the clicked button out of the element array
  const panel = thisButton.nextElementSibling;

  thisButton.addEventListener("click" => {
    if (thisButton.classList.contains("active")) {
      thisButton.classList.remove("active");
      panel.style.display = "none";
    } else {
      thisButton.classList.add("active");
      panel.style.display = "block";
      otherButtons.forEach(button => {
        button.classList.remove("active");
        button.nextElementSibline.style.display = "none";
      });
    }
  });
});
thanks for the help buddy. the fist one got the jib done. it sure takes some time for a beginner like me to learn how to talk to a computer.😁
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom