N
N
Nikolai Kolesnichenko2015-11-18 01:20:37
JavaScript
Nikolai Kolesnichenko, 2015-11-18 01:20:37

Correct slideToggle?

Hello!
I am writing a fairly simple dropdown menu, I can not figure out how to make it work correctly.
Code:
jsfiddle.net/qz55hhde

Here's the problem:
a5f72712c81141678f33131bbea689d5.gif

When you click on another link, the first one doesn't collapse.

Don't kick too hard :) Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-11-18
@Negatiff

Firstly, your markup is not organized in the best way.

<ul class="menu">
    <li>
        <a href="#" class="parrent">item 1</a>
        <ul>
            <li>submenu item 1</li>
            <li>submenu item 1</li>
        </ul>
    </li>
    <li>
        <a href="#" class="parrent">item 2</a>
        <ul>
            <li>submenu item 2</li>
            <li>submenu item 2</li>
        </ul>
    </li>
    <li>
        <a href="#" class="parrent">item 3</a>
        <ul>
            <li>submenu item 3</li>
            <li>submenu item 3</li>
        </ul>
    </li>
</ul>
Well, JS:
$(function () {
    var links = $('.menu a.parrent');
    links.click(function (e) {
        links.not(this).siblings('ul').slideUp(300);
        $(this).siblings('ul').slideToggle(300);
    });
});
Sandbox example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question