0

I'm not JQuery or javascript expert and I have little problem with my website navigation. My navigation looks like typical windows resource management navigation e.g:

Title
    subtitle
    subtitle
        subsubtile
    subtitle
Title
    subtitle

I want it works like same as typical windows resource management navigation, when I click title subtitles will be able to see and when I reclick title it's hide clicked title subtitles.

With the following code I was able to open titles, but don't have any idea how to close them.

$( document ).ready(function() {
    $('#navigation').click(function() {
        $('#navigation ul li:hover').children('#navigation ul li ul').slideDown(400);
    });
})
2
  • first you need to have an idea how to close them, there's lot of ways to do it. Commented May 2, 2013 at 14:50
  • try using slideUp function on mouseout Commented May 2, 2013 at 14:55

1 Answer 1

3

There is a convenient shorthand in jQuery: .slideToggle()

The selected element will be displayed if it's currently not, and vica-versa.

The other important part is, that you can just use $(this) in the event handler. It is the element which the event is handled on.

Also .children() is a tree traversal function. it means it's results will be selected relative to the selected elements it was called on. So $('#example').children('a') will select all links directly under the example element, so no $('#example').children('#example a') needed.

I have wrapped the code up for you:

$(document).ready(function() {
    $('#navigation ul li').click(function(e) {
        e.stopPropagation();
        $(this).children('ul').slideToggle(400);
    });
});

Or if you prefer to have 1 event handler and bubbling:

$(function() {
    $('#navigation').on('click', 'li', function(e) {
        e.stopPropagation();
        $(this).children('ul').slideToggle(400);
    });
});

Of course these two only work if the <ul> you toggle is the direct child of the <li> you fire the click event on.

EDIT:

As @thebreiflabb mentioned in the comments, because of event bubbling the click event will be fired on every parent of the clicked element by default.

If any of the parents also match the selector, the same event handler will be called on them.

e.stopPropagation() tells, that the event is handled here and it does not need to fire on parent elements.

To see the difference click on "Subitem 2" under "Item 4" in both of these fiddles:

With stopping propagation.

Without stopping propagation.

As you can see, without stopPropagation() "Item 4" also closes.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this works, however there is just one small addition because of sub-sub levels and so on. You need to stop propagation to not close the higher up items. jsfiddle.net/mtCyS
I have also incorporated your fiddle in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.