Skip to content
Advertisement

Change icon open on button bootstrap

I am using button with Bootstrap Glyphicon Components and have a lots of buttons on one page, it is using collapse to show hidden section.Here is example codes i have

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#sixts-section-collapse">Prijava <span class="glyphicon glyphicon-menu-down"></span></button>

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#sixts-section-collapse">Prijava <span class="glyphicon glyphicon-menu-down"></span></button>

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#sixts-section-collapse">Prijava <span class="glyphicon glyphicon-menu-down"></span></button> 

What i need is when user click on that button and collapse is shown just to change that icon from down to up, i know i can make it globally for all, but i need to check only for that button and on that button to show another icon. It means when is closed icon down, and when is opened icon up

Advertisement

Answer

$(document).ready(function(){
    $("button[data-toggle=collapse]").click(function(){
        $(this).find('span:first').toggleClass('glyphicon-chevron-down glyphicon-chevron-up')
    });
});

Working Demo

$(".collapse").collapse()
$("button[data-toggle=collapse]").click(function(){
        $(this).find('span:first').toggleClass('glyphicon-chevron-down glyphicon-chevron-up')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container">
    <div class="panel-group" id="accordion">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
              <span class="glyphicon glyphicon-chevron-down"></span> Open
            </button>
          </h4>
        </div>
        <div id="demo" class="panel-collapse collapse in">
          <div class="panel-body">
            Contents:Thank you to help me solve the problem, you're a great guy!
          </div>
        </div>
      </div>
    </div>
</div>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement