Yes, both of them are possible.
Solution to point 1
- You can hide all the tab contents initially, except of course the first tab.
- Then when you click on any one of the tabs, check if its
data-tab
matches the respective tabid
. Useif else
for this. - If you want a link to do this, give it the same
data-tab
and the sameonClick
Listener. - Now, if they match, remove the
current
class from the tab which has this class and add it to the new tab whosedata-tab
matches the tabid
of the tab content sought. - Hide all the tab contents and show only the tab content which has the class
current
Solution to point 2
When a tab is clicked, change the window's location as follows
var href = $(this).data('tab'); window.location.hash = href
this will behave as the url to the tab content
Then when you reload the page, notice everything on the page resets to initial state, but the hash remains consistent.
Now check for the tab content which matches the hash stored in the url as follows
$('.tab-link').each(function(){ var hash = window.location.hash; var href = $(this).data('tab'); if(hash==href) { // set the current tab's class as current // hide all the tab contents and show only current tab's content } });
This can be daunting at first but once you understand it, it's pretty straightforward
Reference
https://api.jquery.com/jQuery.data/
https://developer.mozilla.org/en-US/docs/Web/API/Window.location