1
I’m making an accordion and it’s already functional as I’d like (just a few details I’ll get right later). Only now I need that when clicked and the panel expanded, the + (show more) sign be changed to - (show less).
P.S.: I’m a beginner in Javascript, but the logic is to take the name of the class (or try to pick up) and return a Boolean, if true perform such an action, which is to change the text of a given element.
What do I have:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
.accordion {
background-color: #0c4b7a;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active, .accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
display: none;
background-color: white;
overflow: hidden;
}
.panel p:hover {
background-color: #F47920;
color: white;
}
<h2>Accordion</h2>
<button class="accordion">Título 1 <span style="float: right;font-size: 20px">+</span></button>
<div class="panel">
<p>t is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>
<p>There are many variations of passages of Lorem Ipsum available</p>
</div>
<button class="accordion">Título 2<span style="float: right;font-size: 20px">+</span></button>
<div class="panel">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. </p>
</div>
The following code arrow the class name in the element:
Document.getElementById("myDIV"). classname = "mystyle";
I wanted to type a get and save into a variable and check if it is true, if you are going to perform the above mentioned logic.
Look, the
document.getElementById()
makes clear, "Document.Catchmentpeloid()"... In this case, you must use thedocument.getElementsByClassName()
to be able to take the class of what you want... Examples: "document.getElementsByClassName("output")
"– Jakson Fischer
Yes, but I will take the element by the class and all its children, right? If you have to take the class and perform a filter to only take its name or mount an array to finally check which class it has active ai all right.
– Lucas Inácio
In this case:
document.getElementsByClassName("output").value()
, if you want to do this with Jquery, it is even simpler....$("output).val()
– Jakson Fischer
thanks, but I’m doing it to learn javascript vanilla because I’m wanting to learn Reactjs after.
– Lucas Inácio