Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a glyphicon upon clicking it in Bootstrap 3.2?

I want to have an arrow pointing to the right to allow the user to expand the sidebar, and then change that glyphicon to point to the left. That way, it points to the left so that they understand how to hide the sidebar. I then want it to change back to its default state.

This is what I have currently:

        <div id="page-content-wrapper">
        <div class='hidden-lg'>
        <div class="content-header">
                <h1>
                <a id="menu-toggle" href="#" class="btn btn-default"><i class="glyphicon glyphicon-arrow-right"></i></a>
                </h1>
        </div>
        </div>
like image 663
rb612 Avatar asked Jul 23 '14 04:07

rb612


People also ask

How do I change Glyphicon color and size?

If you need to change the size of glyphicons, use the CSS font-size property. In this snippet, we'll show some examples and explain them. Let's start with an example, where we increase all the glyphicons and set their font-size to 50px.


2 Answers

Just use:

$('#menu-toggle').click(function(){
    $(this).find('i').toggleClass('glyphicon-arrow-right').toggleClass('glyphicon-arrow-left');
});

Fiddle Example

like image 78
apsdehal Avatar answered Oct 25 '22 22:10

apsdehal


Try

$('#menu-toggle').on('click', function(){
    var iSelector = $(this).find('i:first');
    if(iSelector.hasClass('glyphicon-arrow-right')) {
        iSelector.removeClass('glyphicon-arrow-right')
        iSelector.addClass('glyphicon-arrow-left')
    }
});

Fiddle

Reference:

  • selectors
  • on
  • hasClass
  • removeClass
  • addClass
like image 39
Okky Avatar answered Oct 25 '22 23:10

Okky