Hello again! In our next part of our Elementor Tutorial series, we are featuring one of the most commonly asked questions on building Elementor websites: How to show and hide sections in Elementor.
One way to do this is by using the Elementor Tabs widget. The Tabs Widget however, has its limitations in terms of customization of look and feel. Ideally, we want to toggle the visibility of sections with clicks on button widgets that we can customize according to our design.
The image above would be a simple structure we want to achieve. With 3 different customized buttons which shows or hide the sections below based on which one is being clicked on. In your use case, of course, will have a different design and a different number of sections and buttons.
How to Show / Hide Elementor Sections
To achieve this, we will have to use CSS and Javascript. Don’t worry, there’s nothing complicated here, you’ll just need some basic knowledge on how to change some codes to target your buttons and sections.
For context, in this tutorial we will using the Hello Elementor Theme with Elementor Pro installed. This code should work with any Theme for Elementor. However, don’t worry as you won’t be needing Elementor Pro to implement this solution.
Assign IDs to Buttons and Sections
First of all let’s start by assigning CSS IDs to each of the buttons and Sections. For the buttons, click on one of your buttons and click on “Content” Tab, scroll down all the way and you’ll see a “Button ID” field.
Assign an unique ID to this button. Remember, an ID has to be unique so it can only be assigned once per page. Follow the instructions on naming conventions on IDs. You can use a combination of Letters (A-z), Numbers (0-9) and underscore character. Also note that you cannot use spaces in IDs.
Once you have finished doing that, repeat the steps for all your buttons. The ID we use in this example button is “btn1″,”btn2” and “btn3” for simplicity.
Next we have to assign IDs for the sections as well. Click on one of your sections and click on “Advanced” Tab, scroll down all the way and you’ll see a “CSS ID” field.
Don’t worry about the different terminology used, IDs mean the same thing. While you at it, lets assign CSS class called “hidden” as well.
Once you have finished doing that, repeat the steps for all your sections. The ID we use in this example button is “sect1″,”sect2” and “sect3” for simplicity. And remember to add in the “hidden” class to every section as well.
CSS for Hiding Sections
Next, we must define a CSS class for hiding an element. To do so, we use the HTML widget to insert our custom code. Search of “html” in the Elementor search box, if you cannot find the widget.
Place the widget after your buttons and sections. A good practice is to place the HTML widget as the last item of the page. Go to the content tab and paste in the following codes
<style> .elementor-editor-active .hidden{ display:block; } .hidden{ display:none; } .shown{ display: block !important; } </style>
The first CSS rule of this code is to allow the section to be shown by default in Elementor editing screen. While the “.hidden” rule hides the section. Preview the page you are working on and sections should be hidden. We also defined a CSS rule for forcing an element to display.
Javascript for Controlling Behavior
In the same HTML widget, we will also insert in a piece of Javascript to control the behavior of our button clicks.
<script> var btn1 = document.getElementById("btn1"); // replace with your button IDs var btn2 = document.getElementById("btn2"); var btn3 = document.getElementById("btn3"); //Click Event Handlers for buttons btn1.onclick = function(event){ event.preventDefault(); toggleDivs("sect1"); }; btn2.onclick = function(event){ event.preventDefault(); toggleDivs("sect2"); }; btn3.onclick = function(event){ event.preventDefault(); toggleDivs("sect3"); }; //function to hide or show function toggleDivs(s){ //reset document.getElementById("sect1").classList.remove("shown"); document.getElementById("sect2").classList.remove("shown"); document.getElementById("sect3").classList.remove("shown"); //show document.getElementById(s).classList.add("shown"); } </script>
As usual, we are not using JQuery for this. Because, well, we don’t have to depend on JQuery for this interaction. And since we are inserting a Javascript code inside the body of the document, JQuery may not have loaded at this point. So it is more prudent to use vanilla Javascript instead.
To use this code, of course, you’ll need to customize the variables according to your use case. Let’s go through the code:
var btn1 = document.getElementById("btn1"); // replace with your button IDs var btn2 = document.getElementById("btn2"); var btn3 = document.getElementById("btn3");
The top bit of code basically assigns a variable to the Buttons. If you have say 5 buttons, you may want to repeat line 5 times with different assignments to different button IDs.
btn1.onclick = function(event){ event.preventDefault(); toggleDivs("sect1"); };
The next bit of code (above) is an event handler for the button. The function “toggleDivs” is called with an argument “sect1”. The argument, in your case will be the ID of the section you want to show.
function toggleDivs(s){ //reset document.getElementById("sect1").classList.remove("shown"); document.getElementById("sect2").classList.remove("shown"); document.getElementById("sect3").classList.remove("shown"); //show document.getElementById(s).classList.add("shown"); } </script>
The function “toggleDivs” here, is a controller for the Sections. First, by resetting the visibility and then showing the relevent section.
The Full Code
This piece of code is of course not the most efficient code and there’s lots of opportunities for refactoring.
<script> var btn1 = document.getElementById("btn1"); var btn2 = document.getElementById("btn2"); var btn3 = document.getElementById("btn3"); btn1.onclick = function(event){ event.preventDefault(); toggleDivs("sect1"); }; btn2.onclick = function(event){ event.preventDefault(); toggleDivs("sect2"); }; btn3.onclick = function(event){ event.preventDefault(); toggleDivs("sect3"); }; function toggleDivs(s){ //reset document.getElementById("sect1").classList.remove("shown"); document.getElementById("sect2").classList.remove("shown"); document.getElementById("sect3").classList.remove("shown"); //show document.getElementById(s).classList.add("shown"); } </script> <style> .elementor-editor-active .hidden{ display:block; } .hidden{ display:none; } .shown{ display: block !important; } </style>
Elementor Template Download
Download complete project files here! Simply unzip the file, import the layout (.json format) into your Elementor Template library.
Part 2: Toggle Section / Widget Visibility with the same button
Not quite enough? Due to requests from my readers, check out the continuation of the project to include a button active state and toggling section or widget visibility when a button is clicked for a second second time.
Widget Solution
If you however, prefer a Elementor Addons solution, check out this widget by The Plus Addons for Elementor.
Conclusion Show Hide Sections
So there you have it. An easy to way to show and hide Sections in Elementor. We hope that this tutorial will help you master Elementor and Web Design in general. Do check out our other WordPress and Elementor related articles.
Very nice!! im just looking for a static bar in the footer that you can hide by a button (close) similar to the accept cookies bar… how can i use this code to do that?
I’m trying to do this for a section as well, but I’m not succeeding…
Hi Andy, a full code (elementor json file) download has been added in the article. hope this helps!
In a section with 2 columns, is it possible to set a button in column 1 to show/hide column 2?
Possible but cleaner if you were to target an inner section within the 2nd colum instead.
Works as a charm!!!
But I have a question. How can I, for example have section one displayed on screen(unhidden) when I refresh the page, and when I click other sections it will automatically switch between them?
Sure you can! Just add 2 lines of code just before the end script tag like so:
btn1.focus(); //force first button to focus
btn1.click(); //force first button to click
</script> //existing end script tag
Thank you!!! this helped me too!
Hello,
I don’t understand why but it doesn’t work for me. I have followed everything and yet the section does not appear when I click on the button.
Could you help me please ?
Sorry for the bad English, I’m using google translate.
—————————
Bonjour,
Je ne comprend pas pourquoi mais ça ne marche pas pour moi. J’ai bien tout suivis et pourtant la section ne s’affiche pas quand je clique sur le bouton.
Pourriez-vous m’aider s’il vous plait ?
Désolé pour le mauvais anglais, j’utilise Google traduction.
Hi works very well!
Id like to have section 1 activated which would be great, and then of course same functionality as it is, you can switch beteween the buttons.
Is this possbile?
What i mean with “activated” = onse entering the site section1 is allready clicked auto (active)
and then possibility to switch 🙂
Hi Kristian, see answer to Ana above and just add a couple of extra lines of code will do!
Hi there,
I don’t seem to be able to get the second part of this to work on my website. The first part that forces the content to be hidden on the preview website but shown in the editor works perfectly. But I can’t seem to get it so that it works with the buttons in revealing and hiding it?
Any advice would be hugely appreciated. I’m using the Ocean WP theme.
Thanks!
Dylan
Hi Dylan, do try to download and import the full code (added in the article). Hope this helps!
I have to say this is probably the best WP related post I have ever seen. So simple, elegant and amazingly useful. Thank you for sharing.
Is it possible to add a “second click to hide section”
Of course! take a look at part 2 of the article: https://wpjunction.net/elementor-toggle-visibility/
This was SO helpful thank you so much for providing this code & tutorial!
My “reveal/show” button works well, but is there a way to click the same button to”re-hide” what it had revealed?
For instance, the button says “show more” and the section is revealed, and then you can click the same button to “show less” and it collapses again?
Thanks so much!
hello, can you check what the issue is here. I’m trying to use two instances of this code. I have changed ID’s for the second instance of buttons and sections.
Below is the link to the page
http://revamp.coinvest.co.in/index.php/test/
Hi Shyam, You’ll have to update the codebase and all the IDs as well
Hi , first of all very nice snippets , works fine.
I have the same request as Amy.
Is it possible to add a “second click to hide section” again so that only the buttons are visible.
Regards
Wim Bax
Sure! find our how to toggle visibility with the same button here: https://wpjunction.net/elementor-toggle-visibility/
I downloaded your code and installed this for a quick check.
It worked as I want it.
Thank you very much .
I certainly will buy you a cup of coffee/donate
Regards
Wim Bax
Thank you Wim Bax for your kind support!
I have one question. This script works for me fine. but I want to show the section using the slider button. But it does not work with slider. Can you please help me to do this. I’m using the smart slider. I added a button there but I cant see any option to add a button id there.
Hi,
Thank you so much for this.
I have one question, is it possible to only have one section and multiple buttons. Each button shows different text in that one section.
Sure! Just change place the appropriate IDs on the text or header elements. Should work just fine.