Created
August 24, 2016 21:20
Save last bootstrap tabs in localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
// Simply add a data-save-tabs to your tablist e.g. <ul role="tablist" data-save-tabs="your-custom-slug"> | |
$('[data-save-tabs] a[data-toggle="tab"]').on('shown.bs.tab', function (e) { | |
var savedTabs = JSON.parse(localStorage.getItem('savedTabs')); | |
if (typeof savedTabs !== 'object' || !savedTabs){ | |
savedTabs = {}; | |
} | |
savedTabs[$(this).closest('[data-save-tabs]').data('save-tabs')] = $(this).attr('href'); | |
localStorage.setItem('savedTabs', JSON.stringify(savedTabs)); | |
}); | |
$('[role="tablist"]').each(function (idx, elem) { | |
var savedTabs = JSON.parse(localStorage.getItem('savedTabs')); | |
if (typeof savedTabs === 'object'){ | |
for(var index in savedTabs) { | |
if ($(this).data('save-tabs') == index){ | |
$('[href="' + savedTabs[index] + '"]').tab('show'); | |
} | |
} | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment