With tabs lacking the functionality to set the default tab (except to leave the tab open in the designer that you want to default to), here’s a custom solution which ensures the correct tab is open every time you publish the site.
In these examples, the tab links have a class of “.tab-link” and the tab panes are “.tab-pane”
The below piece of code sets the default to none i.e. no tab showing.
Or this works a little faster when the page is rendering:
<script>
// remove default tab and set to none
$(".w-tab-link").removeClass("w--current");
$(".w-tab-pane").removeClass("w--tab-active");
// make the nth-child the default tab
$(".w-tab-link:nth-child(1)").addClass("w--current");
$(".w-tab-pane:nth-child(1)").addClass("w--tab-active");
</script>
Yes, and when you have a lot of tabs on a website, it happens like this: publish, review, notice some tabs are left with wrong defaults, fix, republish…
That is why your scripts are very welcome! And are going to be a great help for 2 of my sites and the one I’m starting in October. It is going to save me a decent amount of time, so thanks a ton
If you want to change the active tab depending on the link a user clicked on a previous page - you can do so by adding a “?test” to the end of the url clicked.
For example - suppose there are two links - one to monthly and another annual pricing options. If the user clicks on the annual link - the annual pricing option content should be the active tab. To do this, add ?annual to the end of the url of the link i.e. /pricing?annual
Then, assuming that the “Annual” pricing is in the 2nd tab on the pricing page, this piece of jquery will make it the active tab.
THIS IS SPECTACULARLY ANNOYING. PLEASE JUST ENABLE A DEFAULT TAB OPTION WEBFLOW. THANK YOU. It makes no sense to have to keep fiddling between the live edit tab, and the default tab (which are clearly not the same thing?!).
Webflow seemed to start prefixing with the w- the classes for tab links and tab panes , causing the selectors for the original script to break. Just update your snippets so it matches the following:
<!-- START FIX: Always show the first tab first -->
<script>
$(".tab-link").removeClass("w--current");
$(".tab-pane").removeClass("w--tab-active");
$(".tab-link:nth-child(1)").addClass("w--current");
$(".tab-pane:nth-child(1)").addClass("w--tab-active");
</script>
<!-- END FIX: Always show the first tab first -->
<!-- Default Active Tab -->
<script>
// Helper function to add a class to an element
function addClass(element, className) {
if (element.classList) {
element.classList.add(className);
} else {
// For older browsers that don't support classList
var classes = element.className.split(' ');
if (classes.indexOf(className) === -1) {
classes.push(className);
element.className = classes.join(' ');
}
}
}
// Helper function to remove a class from an element
function removeClass(element, className) {
if (element.classList) {
element.classList.remove(className);
} else {
// For older browsers that don't support classList
var classes = element.className.split(' ');
var index = classes.indexOf(className);
if (index !== -1) {
classes.splice(index, 1);
element.className = classes.join(' ');
}
}
}
// Remove classes from all tab links and panes
var tabLinks = document.querySelectorAll(".tab-link");
var tabPanes = document.querySelectorAll(".tab-pane");
for (var i = 0; i < tabLinks.length; i++) {
removeClass(tabLinks[i], "w--current");
removeClass(tabPanes[i], "w--tab-active");
}
// Add classes to the first tab link and pane
addClass(tabLinks[0], "w--current");
addClass(tabPanes[0], "w--tab-active");
</script>
I know this is a bit of an old thread, but wanted to chime in. It seems that if you select the Tabs component in the nav tree, then open it’s Settings and use the radio button to select the tab you want to be default, it’ll load that page by default on Chrome/Safari. I used the code here, and it worked - but then I did a little more digging, and found that webflow does this function within the designer. I have Tab one radio button selected, and that’s how it shows up in my preview and custom site.
Possibly there was an update over the last 7 years to include it. That’s good news, right? Weblfow for the win.