Question: Wordpress Close theme menu after click

Question

Wordpress Close theme menu after click

Answers 2
Added at 2016-12-27 18:12
Tags
Question

I am using this Wordpress theme, called ichiban. I have made some custom menu items that links directly to sections on the same page. For these cases I would like the whole menu to un-toggle when the < li > item is clicked. This is the code I have been working on;

jQuery( document ).ready(function($) {

    $('#menu-main li a').on("click", function(){
        $('.site-overlay-wrapper').hide();
    });

});

For now, this code only hides the open menu, the menu button does not reset, and it is not possible to re-ope the menu. Please help me getting this code correct.

SOLUTION

jQuery( document ).ready(function($) {

    $('#menu-main li a').on("click", function(){
        $("body").removeClass("overlay-open");
    });

});

Thank you all :)

Answers
nr: #1 dodano: 2016-12-27 18:12

This help?

jQuery( document ).ready(function($) {
    var t = true;
    $('#menu-main li a').on("click", function(){
        if(t===true){
            $('.site-overlay-wrapper').hide();
            t=false;
        }
        else{
            $('.site-overlay-wrapper').show();
            t=true;
        }
    });

});

Here is working example:

jQuery( document ).ready(function($) {
        var t = true;
        $('#button').on("click", function(){
            if(t===true){
                $('.show').hide();
              $(this).text("SHOW");
                t=false;
            }
            else{
                $('.show').show();
              $(this).text("HIDE");
                t=true;
            }
        });
    
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="button">CLICK</button>
<div class="show">HAPY NEW YEAR!!!</div>

You also can have a problem with $('#menu-main li a'). That a tag is wrong. You need to point click statemant to button class directly because you hide your menu on any a tag inside any li.

nr: #2 dodano: 2016-12-27 19:12

You can try using jQuery .toggle() method.

Change this line:

$('.site-overlay-wrapper').hide();

To:

$('.site-overlay-wrapper').toggle();
Source Show
◀ Wstecz