I want to change its state by clicking on the icon and return it to the previous state by clicking again.
With the first click, the mode I want is activated, but with the second click, no change is made.
Is there a way to define multiple states for the click event instead of one?
The code I wrote is as follows:
let icon = document.querySelector(".test");
icon.addEventListener("click", function() {
if (this.classList.contains("fa-circle-chevron-down")) {
$(icon).addClass("add");
} else {
$(icon).removeClass("add");
}
})
.test {
position: relative;
top: 15px;
left: 25px;
font-size: 50px;
color: #6229ff;
cursor: pointer;
}
.add {
transform: rotate(180deg);
color: #ff3d29 !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fas fa-circle-chevron-down test"></div>
2
Answers
use helper variable for example:
Use
classList.toggle()
to alternate a class.Another option is to alternate between two classes, to get different chevron characters from FontAwesome.