1

I want to link to a bootstrap 3.2.0 tab with a link like this:

<a href="#tab_i_want_to_link" onclick="something()">Tab name</a>

Thanks!!

Christina
  • 34,296
  • 17
  • 83
  • 119
Shalom
  • 15
  • 1
  • 7
  • 1
    possible duplicate of [Bootstrap linking to a tab with an url](http://stackoverflow.com/questions/19625211/bootstrap-linking-to-a-tab-with-an-url) – isherwood Oct 17 '14 at 16:10
  • 1
    Best to not have JavaScript in your HTML. Use an event listener instead. – isherwood Oct 17 '14 at 16:10
  • @isherwood y need this to work with bootstrap 3.2.0, your solution dont active the tab. – Shalom Oct 17 '14 at 16:17
  • With this code: `Tab` i can show the tab, but the tab dont get active, thats way i want to use onclick. – Shalom Oct 17 '14 at 19:35
  • I found the thing I used last year and it wasn't it. Will update answer when I test it. – Christina Oct 17 '14 at 19:53
  • Yes, this works in Bootstrap 3. http://stackoverflow.com/questions/12131273/twitter-bootstrap-tabs-url-doesnt-change/12138756#12138756 – Christina Oct 17 '14 at 19:59
  • thank you @Christina! i'll be waiting! :D – Shalom Oct 17 '14 at 20:00
  • Yes, it's the code in here: http://jsbin.com/IWIKenIn/1#tab3 – Christina Oct 17 '14 at 20:05
  • @Christina the code work for a link in other page, i will use this as well. But the issue is with a link outside the tab in the same page. I think that with a little modification the code can work `jQuery(function(){ var hash = window.location.hash; hash && jQuery('ul.tabNoticias a[href="' + hash + '"]').tab('show'); jQuery('.tabNoticias a').click(function (e) { jQuery(this).tab('show'); var scrollmem = jQuery('body').scrollTop(); window.location.hash = this.hash; jQuery('html,body').scrollTop(scrollmem); }); });` – Shalom Oct 17 '14 at 20:16
  • http://jsbin.com/quvelo/1#tab3 this works for inside outside and just plain works. The freaking script should work like this automatically.Credit: http://timforsythe.com/blog/hashtabs/ – Christina Oct 17 '14 at 21:00

1 Answers1

4

There is a comment thread here: https://github.com/twbs/bootstrap/issues/2415 and NONE of the solutions work as smoothly as this.

SOURCE: http://timforsythe.com/blog/hashtabs/

DEMO: https://jsbin.com/quvelo/2/

This solution links to tabs outside, inside, and wherever you want with a regular url.

  $(window).load(function() { 

    // cache the id
    var navbox = $('.nav-tabs');

    // activate tab on click
    navbox.on('click', 'a', function (e) {
      var $this = $(this);
      // prevent the Default behavior
      e.preventDefault();
      // send the hash to the address bar
      window.location.hash = $this.attr('href');
      // activate the clicked tab
      $this.tab('show');
    });

    // will show tab based on hash
    function refreshHash() {
      navbox.find('a[href="'+window.location.hash+'"]').tab('show');
    }

    // show tab if hash changes in address bar
    $(window).bind('hashchange', refreshHash);
    
    // read has from address bar and show it
    if(window.location.hash) {
      // show tab on load
      refreshHash();
    }
    
});

You put this js AFTER your bootstrap.js inside the functions where you call the tooltip or popover (for example). I have a bootstrap-initializations.js file loaded after bootstrap.min.js in my document.

USAGE: The same as you would use to link to an anchor:

<a href="mypage.html#tabID">Link</a>
Community
  • 1
  • 1
Christina
  • 34,296
  • 17
  • 83
  • 119
  • This makes the tab active but it will go to the top as a regular anchor link does. That's the way it is. – Christina Oct 17 '14 at 17:20
  • Not working on bootstrap 3.2.0, it focus the tab, but dont active the tab. – Shalom Oct 17 '14 at 19:24
  • With this code: `Tab` i can show the tab content, but the tab dont get active, thats way i want to use onclick. – Shalom Oct 17 '14 at 19:33
  • For some reason, sometimes loses the fade in efect between tabs, and the external link to tab load the page scrooled down to the tab. – Shalom Oct 18 '14 at 00:35
  • Yes, the last issue is the timing between jQuery and regular functions, you can try $(window).load(function() { instead of document ready. The other is I don't know why and I'm not investigating. Answering this question has made me switch to Easy Responsive Tabs which is better and responsive and does this stuff automatically. webthemez.com/demo/easy-responsive-tabs/Index.html – Christina Oct 18 '14 at 01:47
  • I removed the fade in and fade and though it doesn't fade it doesn't act odd. I have to re-do all my tabs now for the responsive tabs plugin. – Christina Oct 18 '14 at 02:05
  • `jQuery(window).load(function() { ` solve the fade issue, now this case is closed!! thank you very much!! @Christina – Shalom Oct 18 '14 at 02:33
  • the `jQuery(window).load(function() {` solve the other issue too :D – Shalom Oct 18 '14 at 02:56
  • So this even works when entering the URL#tab inside a browser's address bar? Or will this only work when using links to a certain tabs, when already inside the relevant page? – O0123 Sep 26 '16 at 09:07
  • THANK THE LORD. I had to use $(document).ready(function () { for this to work (Bootstrap 3). Thank you so much. – EFDesign Nov 09 '18 at 16:54