0

Why doesn't this work??

    $("#panellink").click(
    function () {
        $(this).addClass("hover"),
    $("#playerpanel").slideDown(250);
    },
    function () {
        $(this).removeClass("hover"),
    $("#playerpanel").slideUp(250);
    }
);

All I want is when they click on the link the #playerpanel slides down and when clicked again it slides up???

It slides down when I click the link like expected, and when I click the link again it doesn't slide up and just stays there.

0

3 Answers 3

2

Try this instead:

$("#panellink").click(function() {
    $(this).toggleClass( "hover" );
    $(this).slideToggle( 250 );
});
0
1

You want to use toggle instead of click.

http://api.jquery.com/toggle-event/

The .toggle() method binds a handler for the click event, so the rules outlined for the triggering of click apply here as well.

$("#panellink").toggle(
    function () {
        $(this).addClass("hover");
        $("#playerpanel").slideDown(250);
    },
    function () {
        $(this).removeClass("hover");
        $("#playerpanel").slideUp(250);
    }
);

Plus, you had commas after the add/remove class calls...should be semicolons.

I think this will help with the alternating functions that you want to do.

Hope this helps!

0

Let's assume your HTML code is correct, and that you have defined the elements you are using in JQuery (i.e. an element with id="panellink", and one with id="panelplayer").

I'm looking at the .click() event of the JQuery API (http://api.jquery.com/click/), and it doesn't seem to accept a pair of functions as handler.

What you could do is do some checking, and then act on the result. You could check if the element has the hover class or not, and then treat each case.

$("#panellink").click( function() {
    if( $(this).hasClass("hover") ) {
        $(this).removeClass("hover");
        $("#playerpanel").slideUp(250);
    } else {
        $(this).addClass("hover");
        $("#playerpanel").slideDown(250);
    }
)

Note: Also, I just noticed what I presume are typos. Both addClass and removeClass statements should be terminated by a semicolon ;, not a comma ,.

Hope this helps

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.