Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Linking to Specific tab with hash and class="current" Reply To: Linking to Specific tab with hash and class="current"

#199395
Ilan Firsov
Participant

Now that I think about it, it is more complicated than you need since I’m updating the active link on the sidebar as well. Here the code a bit simplified without the bit that updates the sidebar

$(document).on('click', '#myTab a[data-toggle="tab"]', function (e) {
    e.preventDefault();
    window.location.hash = $(this)[0].hash;

    //// tab switch function. this is Bootstrap again
    $(this).tab('show');
});

// this happens on load, we check if the hash exists and if it is show the specific tab, otherwise show default tab
if(window.location.hash) {
    //// tab switch function. this is Bootstrap
    $('#myTab a[href="'+window.location.hash+'"]').tab('show');
} else {
    //// tab switch function. this is Bootstrap again
    $('#myTab a[data-toggle="tab"]:first').tab('show');
}
// we need to know when we change the hash to show the specific tab
$(window).on('hashchange', function(e) {
    e.preventDefault();
    showHashTargetTab();
});