Richard
Richard

Reputation: 57

Refreshing bootstrap tabs

I'm looking for some help with bootstrap tabs.

I have bootstrap tabs with a panel and would like the tab open to remain upon refresh. We have a number of things going on but this is mostly needed as we have new data being inputted via NinjaForms.

Anyway. This is the solution i've found but I cannot get it to work, for some reason..

TABS

 <div class="panel with-nav-tabs panel-default">
    <div class="panel-heading" style="z-index:998;">
      <ul class="nav nav-tabs" id="myTab">
        <li class="active"><a href="#tab1overview" data-toggle="tab"><i 
class="fa fa-building" style="color:#50AE54"></i> Client Overview</a> . 
</li>
        <li><a href="#tab2sites" data-toggle="tab"><i class="fa fa- 
sitemap" style="color:#1DB2C8"></i> Sites</a></li>
        <li><a href="#tab3users" data-toggle="tab"><i class="fa fa-user- 
plus" style="color:#FC5830"></i> Users</a></li>
        <li><a href="#tab4assets" data-toggle="tab"><i class="fa fa- 
desktop" style="color:#2C98F0"></i> Assets</a></li>
        <li class="dropdown">
          <a href="#" data-toggle="dropdown"><i class="fa fa-wrench" 
 style="color:#333333"></i> Services <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#tab11internal" data-toggle="tab"><i class="fa 
 fa-wrench" style="color:#333333"></i> Internal Services</a></li>
            <li><a href="#tab12external" data-toggle="tab"><i class="fa 
 fa-wrench" style="color:#333333"></i> External Services</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="#" data-toggle="dropdown"><i class="fa fa-wrench" 
 style="color:#333333"></i> Web Services <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#tab6webhosting" data-toggle="tab"><i class="fa 
 fa-wrench" style="color:#333333"></i> Web Hosting</a></li>
            <li><a href="#tab7domains" data-toggle="tab"><i class="fa 
fa-wrench" style="color:#333333"></i> Domains</a></li>
            <li><a href="#tab8crushftp" data-toggle="tab"><i class="fa 
  fa-wrench" style="color:#333333"></i> SFTP</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="#" data-toggle="dropdown"><i class="fa fa- 
    certificate" style="color:#333333"></i> Licenses <span 
   class="caret"></span> 
     </a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#tab9email" data-toggle="tab"><i class="fa fa- 
         certificate" style="color:#333333"></i> eMail</a></li>            
          </ul>
        </li>
           <li><a href="#tab10faq" data-toggle="tab"><i class="fa fa- 
   question" style="color:#1DB2C8"></i> FAQ</a></li>
       <!-- Commented out for now <li><a href="#tab10migrations" data- 
     toggle="tab"><i class="fa fa-rocket" style="color:#2C98F0"></i> 
     Migrations</a></li> -->
      </ul>
    </div>

JS

$(function() {
$('a[data-toggle="tab"]').on('click', function(e) {
    window.localStorage.setItem('activeTab', $(e.target).attr('href'));
});
var activeTab = window.localStorage.getItem('activeTab');
if (activeTab) {
    $('#myTab a[href="' + activeTab + '"]').tab('show');
    window.localStorage.removeItem("activeTab");
}
});

WP Functions

wp_enqueue_script( 'tabs-refresh',  
get_template_directory_uri().'/library/js/tabs.js', array( 'jquery' ), 
'1.0.0', false );

Just to add, upon inspection of the console in Chrome. I have found this:

Chrome console error

Any help would be appreciated.

Thanks

Upvotes: 3

Views: 150

Answers (3)

Richard
Richard

Reputation: 57

I actually figured this out.

By simply added this to the tab;

 <li><a href="#tab11internal" **role="tab"** data-toggle="tab"><i class="fa fa- 
 wrench" style="color:#333333"></i> Internal Services</a></li>

Upvotes: 0

Chandru
Chandru

Reputation: 29

When you are using jquery along with WordPress, due to compatibility you must use jQuery and not $

If you want to use document.ready, you can pass $ into the function as a parameter:

jQuery(function ($) {
  // Your code
});

Upvotes: 0

caiovisk
caiovisk

Reputation: 3809

By default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).

See: TypeError: $ is not a function when calling jQuery function

Make your function:

(function($){ 
   [...]
})(jQuery);

So:

(function($){ 
$('a[data-toggle="tab"]').on('click', function(e) {
    window.localStorage.setItem('activeTab', $(e.target).attr('href'));
});
var activeTab = window.localStorage.getItem('activeTab');
if (activeTab) {
    $('#myTab a[href="' + activeTab + '"]').tab('show');
    window.localStorage.removeItem("activeTab");
}
})(jQuery);

Upvotes: 2

Related Questions