1

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

6
  • 3
    You don't need check if (!$(this).hasClass("chatbox-open")) simple else block is sufficeint. I would suggest you to simply use toggleClass() like $(this).toggleClass("fa-chevron-up fa-chevron-down"); Commented Feb 1, 2015 at 14:53
  • Is chatbox-open class on both #openchat and #floatingmenu elements? Commented Feb 1, 2015 at 14:55
  • It looks like you are toggling class chatbox-open for #floatingmenu and 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 Commented Feb 1, 2015 at 15:05
  • yeah, the solution in the bottom didn't work and toggling fa-chevron-up fa-chevron-down does the rick for the chevron toggle but not for the open and close because of the chatbox-open class needs to be present to toggle between open and close. I'll add a jfiddle. Give me a minute to build one quick Commented Feb 1, 2015 at 15:09
  • Question is edited. Added a codepen demo Commented Feb 1, 2015 at 15:16

2 Answers 2

2

Your code only ever goes into the else because #openchat never has its classes toggled elsewhere.

You can just change to this

$("#openchat").click(function () {
    $("#floatingmenu").toggleClass("chatbox-open");
    $(this).toggleClass("fa-chevron-up fa-chevron-down")
});

Live example: http://codepen.io/anon/pen/myBeEb

Sign up to request clarification or add additional context in comments.

Comments

0

actually you don't need that check because if $(this).hasClass("chatbox-open") is false it will go straight to else and execute it.

<script>
$("#openchat").click(function(){
    $("#floatingmenu").toggleClass("chatbox-open");

    if ( $(this).hasClass("chatbox-open") ){

        $(this).removeClass("fa-chevron-up");
        $(this).addClass("fa-chevron-down");

    } else {
            $(this).addClass("fa-chevron-down");
            $(this).removeClass("fa-chevron-up");    
    }        
});
</script>

just remove from else the if (!$(this).hasClass("chatbox-open"))

1 Comment

How does it fix OP's issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.