I have a fiddle for you: http://jsfiddle.net/vSs4f/
I want to show the div.sub-menu
with a simple click on a.haschildren
. If the body loads the div.sub-menu
should be closed. If I click a second time on a.haschildren
the div.sub-menu
should be close.
I have sampled so many things but I think the problems are the lot of DIV's. One idea is in the fiddle.
$(function() {
$("a.haschildren").click(function(e) {
e.preventDefault();
$('div.sub-menu:visible').hide();
$(this).next('div.sub-menu').show();
});
});
I really hope you can help me, thanks!
try it:
Ironically enough, the method you're looking for is .toggle();
http://api.jquery.com/toggle/
just use
toggle()
Personally there are MANY things I would have changed about the structure of your DOM. I am a strong believer that you should base your javascript structure around a well structured DOM, so the traversal is very easy and intuitive. That being said, I'm going to be slightly daring by submitting my fiddle, in the hope that if nothing else, you can look at it and gain a little insight on how to take advantage of a few DOM quirks to make your javascript a bit more intuitive and elegant.
http://jsfiddle.net/vSs4f/6/
Try
Demo: Fiddle
Try this:-
Fiddle
Using
.nextUntil
to reach a point till the.sub-menu
, incase any other siblings come in between this will still work.