I have a background image within a button that I would like to rotate/flip when the button is clicked. The background image is a down arrow, and I would like it to flip to a up arrow. The button is for a collapsible drawer, so I would like the arrow to flip back when the drawer is closed. The button open and closes the drawer, and the background image is applied through CSS.
document.querySelectorAll(".collapsible").forEach((coll) => {
coll.addEventListener("click", () => {
coll.classList.toggle("active");
const content = coll.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
// Add this:
content.style.borderWidth = 0;
} else {
content.style.maxHeight = content.scrollHeight + "px";
// And this:
content.style.borderWidth = "1px";
}
});
});
function myFunction() {
var element = document.getElementById("toggling");
element.classList.toggle("toggled");
};
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition:
border 0.2s ease-out,
max-height 0.2s ease-out;
background-color: white;
border: 0px solid #D1D3D4;
border-radius: 3px;
}
.collapsible {
background-color: white;
color: #021032;
cursor: pointer;
padding: 14px;
width: 100%;
border: solid 1px #D1D3D4;
border-radius: 6px;
text-align: left;
outline: none;
font-size: 15px;
margin: 5px 0px;
}
button {
background-image: url("../images/upArrow.svg");
background-repeat: no-repeat;
background-position: right;
background-size: 25px;
font-weight: 900;
}
.toggled {
background-image: url("../images/downArrow.svg");
background-repeat: no-repeat;
background-position: right;
background-size: 25px;
font-weight: 900;
}
<button type="button" class="collapsible" onclick="myFunction()" id="toggling"> Consult Logs </button>
<div class="content">
<div class="column">
<p>Ensure the disc strength is not at “0”.</p>
</div>
<div class="column">
<img src="../images/discStrength.png" alt="Picture of Logs">
</div>
</div>
<button type="button" class="collapsible" disabled> Ensure All Connectors to Nebuliser Boards are Fully Connected </button>
<button type="button" class="collapsible" onclick="myFunction()" id="toggling"> Loose Crimp Issue </button>
<div class="content">
<div class="column">
<p>Check that the wires and crimps are fully inserted into J1, J4, and trandsducer connectors.</p>
</div>
<div class="column">
<img src="../images/looseCrimp.png" alt="Picture of Connectors">
</div>
</div>