Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
How can I keep selected tab active on page refresh?

My code is not working. This code is basically for keeping active current selected page or tab on page refresh or page reload.

HTML code:

<section id="sidebar" class="refresh">
<a href="#" class="brand">
    <!-- <i class='bx bxs-smile'></i> -->
    <span class="text"><img src="images/logo.png" alt="" class="img-logo"></span>
</a>
<ul class="side-menu top" id="myTab">
    <li class="active">
        <a href="#dashboard" data-toggle="tab">
            <i class='bx bxs-dashboard'></i>
            <span class="text">Dashboard</span>
        </a>
    </li>
    <li>
        <a href="#1a" data-toggle="tab">
            <i class='bx bxs-dashboard'></i>
            <span class="text">Add Employee</span>
        </a>
    </li>
    <li>
        <a href="#2a" data-toggle="tab">
            <i class='bx bxs-group'></i>
            <span class="text">Show Employee</span>
        </a>
    </li>
</ul>
</section>
<section id="content">
<main>
<div class="tab-content clearfix">
        <!-- dashboard -->
        <div class="tab-pane active" id="dashboard">

<!-- some code -->
</div>
 <div class="tab-pane" id="1a">
<!-- some code -->
</div>
<div class="tab-pane" id="2a">
<!-- some code -->
</div>
</main>
</section>

Javascript:

<script>
    $('#myTab a').click(function(e){
        e.preventDefault();
        $(this).tab('show');
        alert("h");
    });

    $("ul.side-menu > li a").on("shown.bs.tab",function (e) {
        var id = $(e.target).attr("href").substr(1);
        window.location.hash=id;
    });
    var hash = window.location.hash;
    $('#myTab a[href="' + hash + '"]').tab('show');
</script>

When I am refreshing the page this code is not working. This code is to keeping active current tab active when the page is refreshed or page reload

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simply add the currently active tab to localStorage. Do this by giving each tab a data attribute like data-index with a value like 1, 2, 3... Then you do localStorage.setItem("tabIndex", $(ul li.active).data(index);) which will save the data-index attribute of the active tab to the browser cache. You can then get the stored value after page load by using this var tabindex = localStorage.getItem("tabIndex");.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!