Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 - Link to specific tab

I have a Bootstrap 4 tabs set in a Wordpress website and want to link to a specific tab from another page link:

Index.php:

<a href="<?php echo get_site_url(); ?>/services/#innovation">Discover More</a>

Services.php:

<!-- Nav tabs -->
            <ul class="nav nav-tabs text-xs-center" role="tablist">
                <li class="nav-item">
                    <a class="nav-link active" data-toggle="tab" href="#innovation" role="tab" aria-expanded="true">
                        <h4>Innovation &<br/>Investigation</h4>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" data-toggle="tab" href="#electronic-engineering" role="tab" aria-expanded="false">
                        <h4>Electronic<br/>Engineering</h4>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link manufacturing" data-toggle="tab" href="#manufacturing" role="tab" aria-expanded="false">
                        <h4>Manufacturing</h4>
                    </a>
                </li>
            </ul>

<!-- Tab panes -->
            <div class="tab-content clearfix">

                <!-- Innovation -->
                <div class="tab-pane active" id="innovation" role="tabpanel" aria-expanded="true">
                    <div data-aos="fade" data-aos-duration="2000" class="col-sm-5">
                        Content
                    </div>
                </div>

                <!-- Electronic Engineering -->
                <div data-aos="fade" data-aos-duration="2000" class="tab-pane" id="electronic-engineering" role="tabpanel" aria-expanded="false">
                    <div class="col-sm-5">
                        Content
                    </div>
                </div>

                <!-- Manufacturing -->
                <div data-aos="fade" data-aos-duration="2000" class="tab-pane" id="manufacturing" role="tabpanel" aria-expanded="false">
                    <div class="col-sm-5">
                        Content
                    </div>
                </div>
            </div>

Javascript:

// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '-tab"]').tab('show');
} //add a suffix

// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})

I had tried this solutions but without success: - Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink

Thanks

like image 383
oyshiu Avatar asked Mar 28 '17 11:03

oyshiu


People also ask

How do I navigate to a single tab of bootstrap 4 from an external link?

You must be using url# or localstorage or cookie or session or other way for save clicked link then used that in index. html page for add . active and . show class to Intended .

How do you open a specific tab via an external link?

tab=x (where x=tab number) to the URL to display the specific tab. For example: https://demo.dj-extensions.com/dj-tabs/?tab=4 will open 4th tab on the demo site, you can try changing the number to another one to see how it works.

How do I create a navigation tab in bootstrap?

Approach: To create a tabbed navigation menu, create a basic unordered list with list items as links. Then add classes nav and nav-tabs of bootstrap to unordered list and nav-items class to list items.

How do I make my current tab active in HTML?

With the help of HTML, CSS, and JavaScript, it is possible to create a navigation menu with a curved active tab. This can be done by using the ::before and ::after pseudo-elements to create the desired shape, and then using JavaScript to add the active class to the element.


2 Answers

I would recommend something like this / use pushState - otherwise your browser scrolls to your ID:

$(function() {
  $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    history.pushState({}, '', e.target.hash);
  });

  var hash = document.location.hash;
  var prefix = "tab_";
  if (hash) {
    $('.nav-tabs a[href="'+hash.replace(prefix,"")+'"]').tab('show');
  }
});
like image 141
dumP Avatar answered Oct 01 '22 07:10

dumP


It's not working because you need to attach the shown.bs.tab handler before calling .tab('show') on page load.

// wire up shown event
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    console.log("tab shown...");
});

// read hash from page load and change tab
var hash = document.location.hash;
var prefix = "tab_";
if (hash) {
    $('.nav-tabs a[href="'+hash.replace(prefix,"")+'"]').tab('show');
} 

Bootstrap 4 - Active Tab on Page Load

Code: http://www.codeply.com/go/Yu8wbjeGMZ
Demo: http://www.codeply.com/render/Yu8wbjeGMZ#tab2

like image 36
Zim Avatar answered Oct 01 '22 08:10

Zim