0

So i'm trying to make a jquery dropdown that adds and removes classes. It does work, but only once. If i try the dropdown again, it simply drops down and slides up right after. It's suppose to drop down after clicking a button and slide up after another click.

$(document).ready(function() {
        //dropdown menu function
        $(".menuDrop").click(function(){
            $(".nav").slideDown("slow");
            $(".menuDrop").addClass("btn-hover");
            $(".menuDrop").click(function(){
                $(".nav").slideUp("slow");
                $(".menuDrop").removeClass("btn-hover");
            });
        });
    });

The "btn-hover" class is meant to add the same css as it where in a hover state once the button is clicked.

--edit--

<div class="menu">
    <div class="col-lg-12">
        <button type="button" class="menuDrop btn btn-default btn-lg">
            <span class="glyphicon glyphicon-chevron-down"></span>
        </button>

        <ul class="nav">
            <li><a class="link" href="#">home</a></li>
            <hr>
            <li><a href="#">portfolio</a></li>
        </ul>
    </div>
</div>
1
  • Can you post the HTML that this goes with? Commented Nov 26, 2013 at 16:18

2 Answers 2

1

Using slideToggle and toggleClass should help you achieve this, and with less code:

//dropdown menu function
$(".menuDrop").click(function(){
    $(".nav").slideToggle("slow");
    $(".menuDrop").toggleClass("btn-hover");
});

Here's a fiddle: http://jsfiddle.net/scesR/

Hope that helps!

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

1 Comment

Well, didn't think of toggleClass. Thanks! Works like a charm :)
0

Bind it using change event, instead of click event.

$(".menuDrop").change(function(){
            $(".nav").slideDown("slow");
            $(".menuDrop").addClass("btn-hover");
            $(".menuDrop").click(function(){
                $(".nav").slideUp("slow");
                $(".menuDrop").removeClass("btn-hover");
            });
        });

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.