Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle button with font awesome and jquery

I thought this was going to be simple, but I am having a bit of hard time getting this to work. I am able to toggle once using .show and .hide, but not able to toggle back.

all the help would be appreciated.

here is the code:

<div class="middle">
    <i class="fa fa-toggle-on fa-2x active" id="on" style="display:none;"></i>
    <i class="fa fa-toggle-on fa-2x fa-rotate-180 inactive" id="off" ></i>
</div> 


$(document).ready(function(){

    $('.middle').click(function(){
        $('.inactive').show();
        $('.active').hide();

    })

    .click(function(){
        $('.inactive').hide();
        $('.active').show();

    });

});

I also have a pen of it here: http://codepen.io/lucky500/pen/qdZPLe

like image 895
Lucky500 Avatar asked Nov 01 '25 02:11

Lucky500


1 Answers

one approach is to use toggle

$(document).ready(function(){
    $('.middle').click(function() {
        $('.inactive, .active').toggle();
  });
});

http://codepen.io/miguelmota/pen/zGqPOX

like image 54
Miguel Mota Avatar answered Nov 02 '25 15:11

Miguel Mota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!