Skip to content

Instantly share code, notes, and snippets.

@0x962
Created January 21, 2020 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x962/ed75f939b61b358643d9593f49bbb8b2 to your computer and use it in GitHub Desktop.
Save 0x962/ed75f939b61b358643d9593f49bbb8b2 to your computer and use it in GitHub Desktop.
Bootstrap Auto Tab Open
$(document).on('ready', function () {
// open tab when page is loaded if hash is present in the URL
let activeTab = document.location.hash || '#details'; // your default tab here
$(`[href='${activeTab}']`).not('.no-auto-tab').tab("show"); // add .no-auto-tab if you don't want it to automatically open
// update location hash as tabs are switched
$('a[data-toggle="tab"]').not('.no-auto-tab').on('shown.bs.tab', function (e) {
e.preventDefault();
window.location.hash = $(e.target).attr("href");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment