0

I'm trying to create a with a "popup" submenu on click.

There's three separate submenus, and I can get them to show with Javascript, except they stack on top of each other. How can I hide the previously shown div when the next one is clicked?

Can I do this with pure CSS?

See this pen for more details.

3
  • yes you can. display:none; in css will hide dom elements! Commented Apr 1, 2014 at 14:39
  • @Ritikesh Yes but he wants the page to be dynamic :-) Commented Apr 1, 2014 at 14:42
  • you can dynamically change css using jquery. Commented Apr 1, 2014 at 14:43

3 Answers 3

1

You need to remember which was opened. Try this for your javascript function:

var current = null; function toggle_visibility(id) { var e = document.getElementById(id); if (current !== null) { current.style.display = 'none'; } if (e == current) { return; } if (e.style.display == 'block') { e.style.display = 'none'; } else { e.style.display = 'block'; current = e; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

You arent toggling the visibility when the same tab is pressed again
Nailed it! Thanks heaps! Ideally I'd like a CSS only solution, but this is good for now.
0

Assign class names to the sub-menus. Before opening a new menu, get the sub-menus by class name, loop over them and hide them. Then open the selected one by id.

Comments

0

Using jQuery you can do something along this line :

 $(".submenu").click(function () { var currentSubMenu = $('.menu').find('.submenu.active'); if (this === currentSubMenu) return; $(currentSubMenu).removeClass('active').hide(); $(this).addClass('active').show(); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.