tags:

views:

9704

answers:

2

I'm new to jQuery and need some help to show and hide a div on click. Basically I need to show a div (containing a small menu) once the user click on a link, and as soon as the user click on a link inside the div just shown. Or clicks outside the div I need to hide the div again.my HTML looks something like this (I'll exist in many places on the page).

<a class="menu" href="#">Menu</a>
<div class="menu-content" style="display: none; position: absolute; border: 1px solid black; background-color: White;padding: 5px;">
 <div><a href="#">Menu option 1</a></div>
 <div><a href="#">Menu option 2</a></div>
</div>

I also have a div that wraps the whole page that I thought I'd set another click-event on to hide the div (to catch clicks outside of the actual menu).

Is this the right approach for solving this and how do I then remove the handlers on the wrapper div etc, etc. What else should I think of to get this right (if it it's the right approach that is)?

Update: Based on the accepted answer below I added this to solve it

//Need to return false here, otherwise the event will bubble up and trigger the hide on body
$('.menu').click(function() { $('.menu-content').hide();$(this).next().show();return false; });
$('body, .menu-content a').click(function() { $('.menu-content').hide();});
$('.menu-content a').click(function() { alert($(this)); });
+1  A: 

Have a look at suckerfish and listamatic. What you trying to achieve may be solvable by CSS only. In addition you'll have gained some insight on how to do your navigation in a more semantic way.

Then you can dive into jQuery for stuff that cannot be done in plain HTML+CSS.

cherouvim
Nice articles but it's not at all what I'm looking for. Don't handle click and uses standard javascript to start with ...
Riri
The script simply fixes IE6's inability to :hover on anything other than a's.
cherouvim
+4  A: 
html:

<a class="menu" href="#">Menu</a>
<div class='a_menu' style="display: none; position: absolute; border: 1px solid black; background-color: White;padding: 5px;">
 <div><a href="#">Menu option 1</a></div>
 <div><a href="#">Menu option 2</a></div>
</div>

script:

$('.menu').click(function() { $(this).next('.a_menu').show(); });
$('body, .a_menu a').click(function() { $('.a_menu').hide(); });

I'd prolly replace the < A > tags with SPANS instead styled with 'cursor:pointer'

Scott Evernden
This will not work when I have it in multiple places within the page
Riri
mighta helped if you'd said that in your Q - you can refine the code i supplied with context and filters on selectors to handle ALL menus on your page
Scott Evernden
@Scott: I did ... "I'll exist in many places on the page". And in you code you only hide the menu on a click on the actual link ... Not outside of the menu.
Riri
yeah ok, i didn't realize a non-precise and fully tested answer that you couldn't use as a starting point was going to lose me reputation. I hope you're happier with the edit. good luck
Scott Evernden
the code i provided btw will hide ALL menus on any click on document body you can add your wrapper div to the list as well if you want
Scott Evernden
@Scott. Removed the down vote. Didn't realize it made you loose rep. Thanks
Riri