Should I use jquery? i want to hide/show p element when i click class faq-q
how does it work?
this is what it look like
html
.faq {
flex-direction: column;
margin-top: 1.5rem;
display: flex;
}
.faq-item {
box-shadow: 0 0 1.25rem rgb(0 0 0 / 8%);
border-radius: 0.3125rem;
background: #e3eaf4;
margin-top: 1.25rem;
}
.faq-q {
padding: 0 3.125rem 0 2.5rem;
font-size: 1rem;
align-items: center;
color: #000;
height: 5rem;
margin-bottom: 0;
font-weight: 600;
cursor: pointer;
display: flex;
justify-content: space-between;
}
.faq-ans {
padding: 0 2.5rem 2.5rem;
}
CodePudding user response:
Should I use jquery? i want to hide/show p element when i click class faq-q
For a simple task like this, I think you don't need jQuery. Just simple querySelectorAll()
and addEventListener()
methods are enough
document.querySelectorAll(".faq-q").forEach(el => {
el.addEventListener("click", (e) => {
el.parentElement.querySelector('.faq-ans > p').style.display =
el.parentElement.querySelector('.faq-ans > p').style.display == "none" ? "block" : "none";
})
});
.faq {
flex-direction: column;
margin-top: 1.5rem;
display: flex;
}
.faq-item {
box-shadow: 0 0 1.25rem rgb(0 0 0 / 8%);
border-radius: 0.3125rem;
background: #e3eaf4;
margin-top: 1.25rem;
}
.faq-q {
padding: 0 3.125rem 0 2.5rem;
font-size: 1rem;
align-items: center;
color: #000;
height: 5rem;
margin-bottom: 0;
font-weight: 600;
cursor: pointer;
display: flex;
justify-content: space-between;
}
.faq-ans {
padding: 0 2.5rem 2.5rem;
}
<div >
<div >
<h5 >When was the company founded?<i ></i></h5>
<div >
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Fugiat porro ipsam delectus, blanditiis expedita cupiditate.</p>
</div>
</div>
</div>