Bootstrap shown.bs.tab event


The shown.bs.event fires when the tab is completely displayed. After that the alert generates as shown below −

$('.nav-tabs a').on('shown.bs.tab', function(){
  alert('New tab is now visible!');
});

The tabs are displayed using the show() method −

$(".nav-tabs a").click(function(){
  $(this).tab('show');
});

You can try to run the following code to implement the shown.bs.tab event −

Example

Live Demo

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </head>

  <body>

    <div class="container">
      <h2>Topic</h2>
      <ul class="nav nav-tabs">
        <li class="active"><a href="#home">Home</a></li>
        <li><a href="#two">Ruby</a></li>
        <li><a href="#three">JSP</a></li>
        <li><a href="#four">Servlet</a></li>
        <li><a href="#five">CSS</a></li>
      </ul>

      <div class="tab-content">
        <div id="home" class="tab-pane fade in active">
          <h3>Home</h3>
          <p>This is demo text!</p>
        </div>
     
      <div id="two" class="tab-pane fade">
        <h3>Ruby</h3>
        <p>This is demo text!</p>
      </div>
      <div id="three" class="tab-pane fade">
        <h3>JSP</h3>
        <p>This is demo text!</p>
      </div>
      <div id="four" class="tab-pane fade">
        <h3>Servlet</h3>
        <p>This is demo text!</p>
      </div>
      <div id="five" class="tab-pane fade">
        <h3>CSS</h3>
        <p>This is demo text!</p>
      </div>
    </div>
  </div>

  <script>
  $(document).ready(function(){
    $(".nav-tabs a").click(function(){
      $(this).tab('show');
    });
    $('.nav-tabs a').on('show.bs.tab', function(){
      alert('New tab will be visible now!');
    });
    $('.nav-tabs a').on('shown.bs.tab', function(){
      alert('New tab is now visible!');
    });
    $('.nav-tabs a').on('hide.bs.tab', function(e){
      alert('Previous tab will hide now!');
    });
    $('.nav-tabs a').on('hidden.bs.tab', function(){
      alert('Previous Tab is hidden now!');
    });
  });

  </script>

  </body>
</html>

Updated on: 18-Jun-2020

738 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements