I am developing a small school management web app.
I have made several tabs
(using bootstrap 3), most of these tabs
have sub tabs
(i.e a single tab has one or more tabs under it).
These sub tabs have forms
that takes data from the user.
The problem is: when a button is clicked, the user is taken to the main home tab.
I want the user to remain on the current tab (where they are currently).
How do i do this?
this is the code (improved)
<!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://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(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 type="text/css">
.bb-example{
margin: 20px;
}
</style>
</head>
<body>
<div class="bb-example">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a data-toggle="tab" href="#sectionA">Section A</a></li>
<li><a data-toggle="tab" href="#sectionB">Section B</a></li>
<li><a data-toggle="tab" href="#sectionC">Section C</a></li>
</ul>
<div class="tab-content">
<div id="sectionA" class="tab-pane fade in 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">
<br>
<div class="row" style="height: 475px;">
<ul class="nav nav-tabs nav-justified" id="myTab">
<li id="li" class="active">
<a href="#Tab1" data-toggle="tab" style="color: blue;"> <span class="glyphicon glyphicon-plus"></span> Tab 1</a>
</li>
<li id="li" >
<a href="#Tab2" data-toggle="tab" style="color: blue;"><span class="glyphicon glyphicon-eye-open"></span> Tab 2</a>
</li>
</ul>
<div class="col-md-11" >
<div class="container" >
<div class="tab-content">
<div id="Tab1" class="tab-pane active">
<div class="row">
<p> Hello there! Tab 1</p>
<br>
</div>
</div>
<div id="Tab2" class="tab-pane">
<div class="row">
<p> Hello there too! Tab 2</p>
<div class="container">
<div class="row">
<div class="col-md-8">
<br />
<form action="tab2.php" method="POST">
<div class="form-group">
<label for="InputUserName"> Enter User Name</label>
<input class="form-control" placeholder="Enter User Name" type="text" name="" id="userName">
</div>
<div class="form-group">
<label for="userName"> Enter Password</label>
<input class="form-control" placeholder="Enter Password" type="Password" name="" id="userName">
</div>
<input type="submit" id="submit" class="btn btn-primary">
</form>
</div>
</div>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>