In this tutorial, you will learn how to close the Elementor tabs by default, on mobile and desktop, on page load.
You will also learn how to make it so that clicking the active tab title closes the tab content. So the tab titles become more of a toggle, like the accordion element behaves. They open / close the tab content, instead of just opening it.
My code here also takes care of updating the aria attributes to their proper values, to respect accessibility standards.
The code you will find here is tested working and will successfully close your Elementor tabs by default, on page load.
This tutorial is exclusively for the new Elementor tabs element. (The one where the content is a container).
Here is a live example of what we will create:
Elementor Tabs Closed By Default Code
Copy paste the code below in an HTML element, on the same page, to close all the Elementor tabs on your page by default, on page load.
Use this code to close the Elementor tabs element on mobile only.
<script> /* Please login to get the code * The code will be for the Elementor Tabs Closed By Default and Make Closable (2024) tutorial * Found at this URL https://element.how/elementor-tabs-closed-by-default/ */ </script>
Use this code to close all tabs on page load, on all viewports.
<script> /* Please login to get the code * The code will be for the Elementor Tabs Closed By Default and Make Closable (2024) tutorial * Found at this URL https://element.how/elementor-tabs-closed-by-default/ */ </script>
To have this one work on desktop only, add:
if (window.innerWidth < 1025) return;
After the line:
setTimeout(function(){
Change the 1025 value to 768 to have it work for desktop and tablet only.
Making the Elementor Tabs Titles a toggle
To change the behaviour of the Elementor Tabs Titles from Opening only the related tab, to toggling it open / close, add this code to your page.
This one's position is important: it needs to be added in an HTML element that's at the end of your page, or at least after all the tabs elements on your page.
<script> /* Please login to get the code * The code will be for the Elementor Tabs Closed By Default and Make Closable (2024) tutorial * Found at this URL https://element.how/elementor-tabs-closed-by-default/ */ </script>
Finally, enjoy your closed by default Elementor Tabs!
I really hope you found this tutorial useful.
Cheers!
15 Responses
One of the best and most needed tutorials on this site! thank you for that.
Welcome!
Wowww you saved me! Thank you so much for this!
Works like a charm
Welcome Erik!
Works but it takes a sec to have an affect, resulting in the tab opening for a split second before closing
Yes. Unfortunately we have to wait until the Elementor JavaScript has loaded.
Thank you!!
Welcome!
This isn't working for me on mobile. I have the tabs widget configured in as a template and inserted into the menu and into a popup using the template widget. I placed the code </body> end in the Elementor custom code feature and made it appear on the entire site. It works in the menu widget on desktop, but not in the popup on tablet/mobile (the items still appear).
Greetings Emily!
Try this, IN the popup template, in a HTML element:
Cheers!
That did it! Thanks so much for the rapid response!!
Ok one more issue... it works on mobile, but now doesn't work on desktop. If I place the original code after the desktop version, then mobile stops working. If I place the code on the popup, mobile works (since the popup is my mobile menu), but desktop doesn't since the code doesn't load until the popup opens. Is there a way to have the code work independently for the desktop and mobile versions? Or to add the Menu widget loading/opening a dropdown be a trigger for the code loading?
Greetings Emily,
Try the following.
In both codes, you will see this line:
let tabsElems = document.querySelectorAll('.e-n-tabs')
In the desktop code, change it to this:
let tabsElems = document.querySelectorAll('.desktopTabs .e-n-tabs')
and then give the class name desktopTabs to your destop tabs element
In the mobile code, change it to this:
let tabsElems = document.querySelectorAll('.mobileTabs .e-n-tabs')
and then give the class name mobileTabs to your mobile tabs element
Let me know if that works.
Cheers!
That works! And as a note to anyone else, I have the tabs setup as a template, and they are inserted on both areas of the website using the template widget. I added the classes above (.mobileTabs, .desktopTabs) to the template widget, not the tabs the template widget was pulling in. So the tabs themselves have no class on them, the template placed in the popup has .mobileTabs and the template placed in the mega menu widget has .desktopTabs. HTML for the mobile version is placed at the bottom of the popup. HTML for the desktop is placed after the menu somewhere. Both are now working correctly and independently from one another. THANK YOU!!
Thanks for the added clarification for others!