Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Keep Last Selected Bootstrap Tab Active on Page Refresh</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <script> $(document).ready(function(){ $('a[data-toggle="tab"]').on('show.bs.tab', function(e) { localStorage.setItem('activeTab', $(e.target).attr('href')); }); var activeTab = localStorage.getItem('activeTab'); if(activeTab){ $('#myTab a[href="' + activeTab + '"]').tab('show'); } }); </script> <style> .bs-example{ margin: 20px; } </style> </head> <body> <div class="bs-example"> <ul class="nav nav-tabs" id="myTab"> <li class="nav-item"> <a href="#sectionA" class="nav-link active" data-toggle="tab">Section A</a> </li> <li class="nav-item"> <a href="#sectionB" class="nav-link" data-toggle="tab">Section B</a> </li> <li class="nav-item"> <a href="#sectionC" class="nav-link" data-toggle="tab">Section C</a> </li> </ul> <div class="tab-content"> <div id="sectionA" class="tab-pane fade show active"> <h3>Section A</h3> <p>Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth.</p> </div> <div id="sectionB" class="tab-pane fade"> <h3>Section B</h3> <p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p> </div> <div id="sectionC" class="tab-pane fade"> <h3>Section C</h3> <p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p> </div> </div> </div> </body> </html>