1

Im working on function to toggle a menu on click but cannot make it work since it got deprecated with jQuery 1.9.

I have no idea how to manage to make work.

I want to click on the menu img once and expand the menu and the next time I do that it will hide again.

This is my code:

$(document).ready(function(){
$('.menu img').click(function(){
    var wWidth = $(window).width();
    var wHeight = $(window).height()
    $('.menu').animate({width: wWidth + 'px', height: wHeight + 'px'}, 500);
    $('.menu').toggleClass('menu_expanded');
});

Thanks in advance.

Here is the JSfiddle as request: http://jsfiddle.net/czef8ofb/

2
  • 1
    Could you give us a JSFiddle or a link so we can see the problem in action? Commented Aug 15, 2014 at 19:26
  • What's the problem with the code you have? Commented Aug 15, 2014 at 20:02

2 Answers 2

1

You could try the following code – .hasClass() checks whether your navigation element has already been assigned the .menu_expanded class and shows/hides the menu accordingly.

$(document).ready(function(){
    $('.menu img').click(function(){
        var wWidth = $(window).width();
        var wHeight = $(window).height()
        if(!$('.menu').hasClass('menu_expanded')){
            $('.menu').animate({width: wWidth + 'px', height: wHeight + 'px'}, 500);
        }else{
            //code to hide menu again
        }
        $('.menu').toggleClass('menu_expanded');
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to add an explanation so that others can learn something.
Isn't .hasClass()rather self explanatory? :/
@Fyxerz I have made a little change to the code, it should work now.
0

According to jQuery docs:

This method signature was deprecated in jQuery 1.8 and removed in jQuery 1.9. jQuery also provides an animation method named .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of arguments passed.

So, use this one instead: animation method .toggle()

2 Comments

Yes, but it seems to me, he doesn't know yet, that there is another .toggle() that replaces deprecated one.
I know that there is an animation. The toggle I used to use was the event toggle not the animation one. The code below suggested by Marcel works just fine. A pitty they took toggle out...

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.