Search code examples
jquerycssbuttonfont-awesomepseudo-class

Button Toggle using :before :after with Font Awesome Icons


I have a toggle button that uses the bars Font Awesome icon and I'm using the :after pseudo class to add a left caret to the left side of the bars. When I click the button, I want the left caret to go away and a right caret on the right side of the bars to appear. An example of this functionality is seen on the One Codex Beta Site (on that site it starts with a right caret on the right side of the bars, whereas I'm starting with a left caret on the left side of the bars).

I've Googled for help on how to do this, but I haven't been able to get this to work as I want it to. Any ideas would be much appreciated. Thank you!

HTML

<div id="header" class="navbar navbar-default navbar-static-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#nav-collapse">
                    <span class="sr-only">Toggle Navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <div id="logo" class="navbar-brand">
                    <button type="button" id="toggleSidebar">
                        <i class="navbar-icon fa fa-bars icon"></i>
                    </button><a href="index2.html" class="brand noline" title="Home">Brand</a></div>
            </div>
            <div class="collapse navbar-collapse" id="nav-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li id="nav-join"><a href="#" id="join" title="Join">Sign Up</a></li>
                    <li id="nav-signin"><a href="#" id="sign-in" title="Sign In">Sign In</a></li>
                    <li id="nav-about-us"><a href="#" id="about-us" title="About Us">About Us</a></li>
                </ul>
            </div>
        </div>
    </div>

JS

$(document).ready(function(){
            $.asm = {};
            $.asm.panels = 2;

            $('#toggleSidebar').click(function(){
                if ($.asm.panels === 1) {
                    $('#content').attr({'class': 'products col-lg-9'});
                    $('#sidebar').show();
                    $.asm.panels = 2;
                } else {
                    $('#content').attr({'class': 'col-lg-12'});
                    $('#sidebar').hide();
                    $.asm.panels = 1;
                }
            });
        });

CSS

.fa-bars:after {
  content: "\f0d9";
  float: left;
  padding-right: 5px;
}

#content {
    background-color: gray;
    padding: 0px;
    height: 100px;
}

Here is my JSFiddle


Solution

  • You need to add a class to rewrite the content for the icon, i.e:

    JS

    $('#toggleSidebar').click(function(){
    $(this).find('.navbar-icon').toggleClass('right');
    

    CSS

    .fa-bars.right::after {
    content: "";
    float: right;
    padding-left: 5px;}
    

    https://jsfiddle.net/5xtmapqp/