How to make toggle button switch use boostrap 5?

Viewed 1369

I have a proble to make a toggle switch button, and I do not know how to do it. Here is my code that I try to do, I hope someone can help me:

<div class="btn-group" checked data-toggle="toggle" role="group" aria-label="Basic mixed styles example">
            <button type="button" class="btn btn-danger">Month</button>
            <button type="button" class="btn btn-danger">Year</button>
</div>

Toggle switch like this: Toggle switch change between month and year

2 Answers

Assumed you would be using jquery (bootstrap), Ypu can further style as you see fit

(function($) {
  'use strict';

  $(document).ready(() => {

    $("input[type='button']").click((event) => {
      $("input[type='button']").toggleClass("btn-toggle");
    });

  });

})(jQuery);
input[type='button'].btn-toggle {
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<div class="m-5" role="group" aria-label="Basic mixed styles example">
  <input type="button" class="btn btn-toggle" value="Month">
  <input type="button" class="btn" value="Year">
</div>

Bootstrap 5 uses data-bs-toggle instead of data-toggle

Related