DEV Community

Discussion on: How to show the clicked element only and hide others in Vanilla JavaScript?

Collapse
 
scrabble96 profile image
Scrabble96

I have found this very useful but I'd like to take it one stage further: as well as hiding the content relating to the other buttons when clicked, I'd like it to hide the content when clicking the open one again. So:

  1. Hide all content to begin with (like sub-menus in a menu).
  2. Click on a button to reveal its content
  3. Click on the same button to hide its own content again so all content is hidden (whilst maintaining the function you have above to close the content of another button when a different button is clicked)

I am going round in circles trying to achieve this with no success.

Collapse
 
mohdhussein profile image
Mohammed

Hey. I haven't figured a way to do number 3. If I came up with a solution I'll post it.