Hi, I have an icon that I want to use to toggle a divs visiblity.
Initially the div is hidden (display: none), once the icon is clicked, the div should appear.
And when the icon is clicked again, the div should be hidden again.
The icon is located outside the div.
I tried using two click interactions on the icon to show and hide the element, targeting the div with a CSS selector. It works to hide it, or to show it, but not to toggle it.
I replicated this but can’t seem to get this to work. Seems like an issue with display: none, cause if it starts out visible then it does work but if it starts with display:none it does not.
I had the same problem, then I realized that the css on the element overwrites the css on the class.
What I did was remove the display:none on the element, then create a class called “Hidden” with the display:none, and set the element to that class initially. That means that when you toggle the class, the element will become visible.
How do you initially set the class to “hidden”?
I can get it to hide when the element is showing first. In the CSS under the element to be hidden as such:
.nav-unhidden {
display: block; }
.nav-hidden {
display: none }
The toggle/button is Click | Toggle Attribute | .nav-unhidden > target CSS selector class
But can’t get it too initially be invisible. I’m assuming the toggle will have to change if I figure this out, because right now it is toggling the .nav-unhidden, and will need to change to .nav-hidden.
That’s the simple version. I also created a version that fades it in with animations, but that required a bit more work. First I had to show it, then the animation, then set a key in session storage to show that it was visible so it wouldn’t be possible to click twice and get wonky results.
Then I did the reverse when hiding the element: fade out + hide + remove key from browser storage.
Lastly, I had to remove the key on page load, otherwise it would think that the element was visible if you showed it and then reloaded the page.
I probably messed it up and there’s a simpler solution, but this is what worked for me