I have a small panel that slides out from the bottom. It has a chevron up icon. I am building a jquery snippet that bring it up (opens it) and closes it but all it must change the chevron from up to down.
The opening and closing is working and the chevron changes when it opens but it doesn't reset back to chevron up when it closes. Something is wrong in my conditional statement.
This is what I have so far
<script>
$("#openchat").click(function(){
$("#floatingmenu").toggleClass("chatbox-open");
if ($(this).hasClass("chatbox-open")) {
$(this).removeClass("fa-chevron-up");
$(this).addClass("fa-chevron-down");
} else if (!$(this).hasClass("chatbox-open")) {
$(this).addClass("fa-chevron-down");
$(this).removeClass("fa-chevron-up");
}
});
</script>
I am attaching a CODEPEN DEMO
BTW, my .chatbox-open class is what opens it and closes it. The other classes are simple font-awesome classes for the icons
Any help please
if (!$(this).hasClass("chatbox-open"))simple else block is sufficeint. I would suggest you to simply usetoggleClass()like$(this).toggleClass("fa-chevron-up fa-chevron-down");chatbox-openclass on both#openchatand#floatingmenuelements?chatbox-openfor#floatingmenuand checking it for#openchat. Anyway, you have to provide mininmalistic sample code to replicate issue, e.g a jsFiddle and all relevant HTML markup in question