0

I am writing this code, first toggle function is working but second toggle isn't. Why is that?

<script type="text/javascript">
    $(document).ready(function(){
        $( ".navicon" ).click(function() {
            $( "#menu" ).slideToggle( "medium", function() {
            // Animation complete.
            });
        });
    });
    $(document).ready(function(){
        $('.navicon').toggle(function () {
            $("body").css({"overflow": "hidden !important"});
            $("#mobile_menu").css({"width": "100% !important"});
        });
    });
</script>
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • 2
    wrap all in one document.ready. – Wilfredo P Aug 20 '14 at 12:34
  • Can you provide more details on "what's working" aswell as the HTML of the page? – Luca B. Aug 20 '14 at 12:34
  • Do you get any errors in the console? – Huangism Aug 20 '14 at 12:56
  • Try `$("#mobile_menu").css({"width": "100%"});` without the important. See http://stackoverflow.com/questions/2655925/apply-important-css-style-using-jquery and http://stackoverflow.com/questions/1986182/how-to-include-important-in-jquery I have voted to close the question as it is answered else where – Huangism Aug 20 '14 at 13:02
  • possible duplicate of [apply !important CSS style using jQuery](http://stackoverflow.com/questions/2655925/apply-important-css-style-using-jquery) – Huangism Aug 20 '14 at 13:04

1 Answers1

1

The toggle() method was deprecated, you need to use an alternative, such as a flag to depict the 'toggled' state.

A loose example:

$('.myclass').click(function() {
if ($(this).hasClass('toggle')) {
doThis;
$(this).removeClass('toggle');
} else {
doThis;
$(this).addClass('toggle');
}
});

And encase all of your functions with $(document).ready() rather than doing it for each individual one.

Voltlight
  • 136
  • 6
  • It was the `toggle` overload that takes two (or more) functions as parameters that was deprecated. The animation overloads are still there. – Guffa Aug 20 '14 at 12:43
  • I guess documentation wasn't very clear on this, I struggled with it before and this solution was suggested to me. Interesting to know, though. – Voltlight Aug 20 '14 at 12:46