How to activate not default Bootstrap tab from outer page? - javascript

I have which have two links. First goes to page with default activated tab, and second link - to second tab.
I alway get default tab activated.
Here is my js code(opens tab #home):
$('.activate_tab').click(function(e){
$('#myTab a[href="#profile"]').tab('show');
});
tried also(not doing anything):
$('.activate_tab').click(function(e){
e.preventDefault();
$('#myTab a[href="#profile"]').tab('show');
});
and my default Bootstrap HTML code:
<ul id="myTab" class="nav nav-tabs">
<li class="reports active left"><a href="#home" data-toggle="tab" >Details</a></li>
<li class="reports right not_active" >Analytics</li>
</ul>
...
<div class="tab-pane fade in active" id="home">//default
...
<div class="tab-pane fade" id="profile">//not default
...
//link on other page, which should activate default( home )tab
http://super.com
//link on other page, which should activate not default tab
<img alt="" height="19" src="http://127.0.0.1:3000/assets/analytics.png" >
What I'm missing guys ?

I changed my link:
aaa
and my JavaScript:
var activeTab = $('[href=' + location.hash + ']');
activeTab && activeTab.tab('show');
$('.activate_tab').click(function(e){
$('#myTab a[href="#profile"]').tab('show');
});

try this
<div class="tab-pane fade" id="home">//default
...
<div class="tab-pane fade in active" id="profile">//not default
or you can try on your second link page
$('#myTab a:last').tab('show'); // Select last tab
$('#myTab li:eq(1) a').tab('show');

Related

How to link to a specific tab on a page | Bootstrap & Rails

I have an about page with different tabs that I want to link to individually through my footer. I've checked out other solutions to this but I'm not sure of two things. I've tried to use the following javascript to solve this, but I'm not sure how to configure what is here:
A) How do I configure the JavaScript to work with my HTML?
B) Do I need to duplicate the following JS for each tab on the page?
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('/info#advertise-tab')) {
$('.nav-tabs li a[href="info' + url.split('#')[1] + 'advertise-tab"]').tab('shown.bs.tab');
}
// Change hash for page-reload
$('.nav-tabs li a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})
My page's view:
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" id="advertise-li">Advertise</li>
<li role="presentation" id="legal-li">Legal</li>
</ul>
<div class="tab-content" id="infoTabContent">
<div role="tabpanel" class="tab-pane fade" id="advertise-tab">
<p>stuff</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="legal-tab">
<p>other stuff</p>
</div>
</div>
Page footer links:
Advertise
Legal
Here is the jsfiddle

Is it possible to add links to bootstrap 4 nav-pills tab-panel from a different page?

I have a service page that using bootstrap 4's pills .nav-pills navigation (this is not the main navigation navbar), which is working very well. My challenge is that i want to be able to click on a link on the home page that should open the targeted tab-panel on the service page. Haven't been able to find a way for almost a week now. How can i achieve this?
Service.html
<div class="nav flex-column nav-pills col-md-3" id="v-pills-tab" role="tablist" aria-orientation="vertical">
<ul class="list-group list-unstyled">
<li class="nav-item">
<a class="nav-link list-group-item" id="v-pills-manpower-tab" data-toggle="pill" href="#v-pills-manpower" role="tab" aria-controls="v-pills-manpower" aria-selected="false">Manpower Supply</a>
</li>
</ul>
</div>
<div class="tab-content col-md-8" id="v-pills-tabContent">
<div class="tab-pane fade" id="v-pills-manpower" role="tabpanel" aria-labelledby="v-pills-manpower-tab">
<h5>Man Power Supply</h5>
</div>
Home.html
<a href="services.html#v-pills-manpower" data-toggle="tab" >
My code in home.html doesn't work but that's one of my many attempts.
On the home page, configure data attributes to help. I've added data-tab-name to the anchor and it defines the desired, selected tab for the service page.
When the page loads, it re-configures the onclick of the link so that the tab name can be stored in localStorage before redirecting the user to the service page.
Home.html
<a href="services.html" data-tab-name="v-pills-manpower" data-toggle="tab" >
<script type="text/javascript">
window.onload = function() {
document.querySelectorAll('[data-toggle="tab"]').forEach(function(item, index){
item.addEventListener('click', function(e){
e.preventDefault();
localStorage.selectedTabName = item.getAttribute('data-tab-name');
window.location.href = item.href;
});
});
};
</script>
When the service page loads, the javascript looks for the saved tab name in localStorage, identifies that tab and then makes it the active tab by applying the "active" class to the appropriate element.
Service.html
<script type="text/javascript">
window.onload = function() {
document.querySelectorAll('.nav-link').forEach(function(item, index){
var isActive = (item.className.indexOf('active')>-1);
if (item.id==localStorage.selectedTabName) {
if (!isActive) {
item.className += ' active';
}
item.setAttribute('aria-selected', 'true');
} else {
item.setAttribute('aria-selected', 'false');
if (isActive) {
item.className = item.className.replace('active', '');
}
}
});
};
</script>

Bootstrap Tabs - Open particuler tab on a different internal side and jump to anchor by click

Hi all Regarding this post I could solve my problem opening a new page and a particuler tab by using this script
window.onload = function(){
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href=#' + url.split('#')[1] + ']').tab('show');
}
//Change hash for page-reload
$('.nav-tabs a[href=#' + url.split('#')[1] + ']').on('shown', function (e) {
window.location.hash = e.target.hash;
});
}
The link looks like this
index.php?content=seminare#wppp
I'm using a php include function. In this case I include seminare.php into index.php and open tab WPPP.
The code of the tabs look like this
<ul class="nav nav-tabs responsive-tabs">
<li class="active">
<a data-toggle="tab" href="#monitor"><i class="fa fa-circle" aria-hidden="true"></i>Monitor</a>
</li>
<li>
<a data-toggle="tab" href="#wppp"><i class="fa fa-circle" aria-hidden="true"></i>WPPP</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade in active" id="monitor">
content 1
</div><!-- tab-group -->
<div class="tab-pane fade in" id="wppp">
content 2
</div><!-- tab-group -->
</div>
By now I end up on the top of the page without the focus on the open tab.
So I have to scroll down to the tab. How can I manage to jump to the tab area as well as like the normal anchor functionality?
Thanks guys.
I'd use jQuery to do this easily:
$(function() {
$(document).scrollTop( $("#wppp").offset().top );
});
But make sure its visible though.
Problem solved. Because the particuler div was hidden it couldn't work.
if (url.match('#')) {
$('.nav-tabs a[href=#' + url.split('#')[1] + ']').tab('show');
$('#' + url.split('#')[1]).addClass('active');
}
Add class 'active' and it works

Changing active Bootstrap 3 tab using jQuery

I was experimenting with programatically changing tabs using jQuery. I tried to implement the solution given by #MasterAM here. However, the Chrome console shows the error Uncaught TypeError: undefined is not a function
Here is my HTML:
<div id="click-me-div">Click Me</div>
<ul class="nav nav-tabs">
<li class="active">I like Fruits</li>
<li>I like Veggies Too</li>
<li>But Samosa's Rock</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="fruits">Apple, Kiwi, Watermellon</div>
<div class="tab-pane" id="veggies">Kale, Spinach, Pepper</div>
<div class="tab-pane" id="samosas">Awesome, Spine Tingling, Explosive</div>
</div>
And here is the jQuery:
$(document).ready(function() {
$("#click-me-div").click(function() {
alert('yes, the click actually happened');
('.nav-tabs a[href="#samosas"]').tab('show');
});
});
The desired outcome is that, if I click "Click Me", the active tab should change to the "samosas" tab.
What am I doing wrong?
You left out the jQuery function
$(document).ready(function() {
$("#click-me-div").click(function() {
alert('yes, the click actually happened');
$('.nav-tabs a[href="#samosas"]').tab('show');
});
});
Fiddle
Try this code
You can use onclick() function and path id name of tab.
<a onclick='ActivTab("Security")' class="nav-link" data-toggle="tab" href="#Security" aria-controls="profile" role="tab" id='Security'>Security</a>
<script>
$(document).ready(function(){
ActivTab('Security');
});
function ActivTab(id){
// $('.nav-tabs a[href="#' + id + '"]').tab('show');
$('.nav-tabs a[href="#' + id + '"]').trigger('click');
};
</script>

Bootstrap: Tab doesn't show content correctly

In my page i use bootstrap's tab, but when i click each of tab, content of tab doesn't show correctly, this is my code:
<div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;">
<ul id="myTab" class="nav nav-tabs nav-justified">
<li class="active">Home</li>
<li>Favoriets</li>
<li>Friends</li>
<li>Experience</li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="tab-home">
HOME
</div>
<div class="tab-pane fade active" id="tab-fav">
My favoriets
</div>
<div class="tab-pane fade active" id="tab-fr">
My Friends
</div>
<div class="tab-pane fade active" id="tab-ex">
My experience
</div>
</div>
<script type="text/javascript">
$(function () {
var navTabs = $('.nav-tabs a');
var hash = window.location.hash;
hash && navTabs.filter('[data-value="' + hash + '"]').tab('show');
navTabs.on('shown', function (e) {
var newhash = $(e.target).attr('data-value');
window.location.hash = newhash;
});
})
</script>
</div>
JSFIDDLE
Where am i wrong? How can i fix it?
You have active class on all tab-pane divs. Remove it from all of them but the first one. BTW, in bootstrap 3, the event it is not show, it is shown.bs.tab Bootstrap 3 tabs.
I personally use this code: Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink, the only change I made, was to replace the on('show' with on('shown.bs.tab' .
On your JSFiddle a reference to the jQuery library is missing. Once adding a jQuery reference under Frameworks & Extensions it's switching tab content.
Under the Bootstrap documentation is states that jQuery is required for bootstrap javascript features: http://getbootstrap.com/getting-started/#whats-included
Also, your function is being using jQuery notation.

Categories