Mutasim Fuad
Mutasim Fuad

Reputation: 606

Dynamically set active tab with jquery

I'm trying to set a tab active dynamically. I have search the net but the solutions is not working.Here is my code.

<ul id="memnav" class="nav nav-tabs">
    <li class="active">
    <a class="dropdown-toggle" data-toggle="tab" href="#publication-tab">
        Publications
    </a>
    </li>
    <li><a data-toggle="tab" href="#project-tab">Projects</a></li>
    <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        About me
        <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
        <li><a data-toggle="tab" href="#education-tab">Education</a></li>
        <li><a data-toggle="tab" href="#career-tab">Career</a></li>
        <li><a data-toggle="tab" href="#contact-tab">Contact</a></li>
    </ul>
    </li>
</ul>

and in the script

var active_tab = sessionStorage.getItem('active_tab');
        if(active_tab){
            $("#memnav").tabs().tabs( "option", "active", active_tab);
        }

But it throws this error

Uncaught Error: jQuery UI Tabs: Mismatching fragment identifier.

I have also put the "ul" inside a "div" but it shows the same error

Upvotes: 0

Views: 10874

Answers (2)

thephpx
thephpx

Reputation: 522

You can try this as well :)

$(document).ready(function(){
	var active_tab = '#publication-tab';
	$('#memnav').find('a[href="'+active_tab+'"]').parent().addClass('active');
});
<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>
<ul id="memnav" class="nav nav-tabs">
    <li>
    <a class="dropdown-toggle" data-toggle="tab" href="#publication-tab">
        Publications
    </a>
    </li>
    <li><a data-toggle="tab" href="#project-tab">Projects</a></li>
    <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        About me
        <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
        <li><a data-toggle="tab" href="#education-tab">Education</a></li>
        <li><a data-toggle="tab" href="#career-tab">Career</a></li>
        <li><a data-toggle="tab" href="#contact-tab">Contact</a></li>
    </ul>
    </li>
</ul>

Upvotes: 0

לבני מלכה
לבני מלכה

Reputation: 16251

simply use addClass
add .eq() :https://api.jquery.com/eq/

$(document).ready(function(){
var i=0;//get from storrage
$("#memnav li").eq(i).addClass('active');
});
  <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>
<ul id="memnav" class="nav nav-tabs">
    <li>
    <a class="dropdown-toggle" data-toggle="tab" href="#publication-tab">
        Publications
    </a>
    </li>
    <li><a data-toggle="tab" href="#project-tab">Projects</a></li>
    <li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        About me
        <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
        <li><a data-toggle="tab" href="#education-tab">Education</a></li>
        <li><a data-toggle="tab" href="#career-tab">Career</a></li>
        <li><a data-toggle="tab" href="#contact-tab">Contact</a></li>
    </ul>
    </li>
</ul>

Upvotes: 1

Related Questions