hamed Jafarian
Coder
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: