1

I have a simple function to show a dropdown menu.

This is a responsive menu, it only works with defined sizes on my media queries, that's why I want to hide submenu after click.

I want to click on one of the links and then this dropdown menu hides. I am aware of .hide() but I can't get it to work.

Any help?

I want to keep this code simple & clean.

HTML

<nav>
            <ul class="navigation">
                      <img src="img/menu.png" alt="mobile" width="50" height="50"/>
                        <li class="n1"><a href="#home">Principal</a></li>
                        <li class="n2"><a href="#services">Serviços</a></li>
                        <li class="n3"><a href="#team">Equipa</a></li>
                        <li class="n4"><a href="#contact">Contactos</a></li>
            </ul>
            <span>Euclides Style | 965648044</span>
        </nav>

Javascript

$("nav").click(function () {
    $(".navigation").toggleClass('open');
});

UPDATE

I used a simple solution:

$(".navigation a").click(function () {
            $(".navigation").removeClass('open');
    });

Thanks everyone for your help!

1
  • Show us the HTML also Commented Apr 10, 2014 at 10:36

6 Answers 6

1

hide() is working. Try:

$("nav li").click(function () {
    $(".navigation").hide();
});

DEMO

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

1 Comment

can you please help me to fix this stackoverflow.com/questions/51552712/… @laaposto
1
$("a").on("click", function (e) {
    e.preventDefault();
    $(".navigation").fadeOut();
});

or you can try .hide() even .fadeOut();

1 Comment

This removes all my ".navigation" but I edited it to ".navigation .open". Thanks
0

Assuming that open class has display: none or display: block attribute (it depends on the starting state) will work fine.

Alternatively you can use toggle function.

Demo: http://jsfiddle.net/IrvinDominin/uQg2X/

1 Comment

can you please help me to fix this stackoverflow.com/questions/51552712/… @IrvinDominin
0
$("nav").click(function () {
   $(".navigation").toggleClass('open');
});

and you should add some css

   .navigation{ display:'none'}
   .navigation.open{display:'block'}

Comments

0

First: it is not allowed to have a img-tag directly inside ul-tag. This is an example of a working code with some modifications:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hiding/showing navigation</title>

<style>
.closed{
    display: none;
    }
</style>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
    $("nav").click(function(){      
        $(".navigation").toggleClass('closed');
    });
});
</script>

</head>
<body>
<nav>
    <h1>The nav</h1>
    <ul class="navigation">                      
        <li class="n1"><a href="#home">Principal</a></li>
        <li class="n2"><a href="#services">Serviços</a></li>
        <li class="n3"><a href="#team">Equipa</a></li>
        <li class="n4"><a href="#contact">Contactos</a></li>
    </ul>
    <span>Euclides Style | 965648044</span>
</nav>
</body>
</html>

1 Comment

Since when its not allowed? It's used quite more often then you think. Anyways, thanks for your help
0

The hide(); solution will totally hide the menu and will not show again the next time, the toggle solution will toggle all the other menus on the page, so here below is my solution, this effect will apply to all your dropdown menus on a page.

$(document).on("click",".dropdown-menu li a", function(){
    jQuery(this).parent('li').parent('ul').parent('div').removeClass('open');
});

Comments

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.