• Resolved almirdzankovic

    (@almirdzankovic)


    Hi!

    I am making a child theme for Storefront, and I reload some pages with my own AJAX. After then AJAX, the mobile main menu toggle button stops working. So I have called the following script that I got from the Storefront theme, after my AJAX:

    // eslint-disable-next-line @wordpress/no-global-event-listener
                const container = document.getElementById( 'site-navigation' );
                if ( ! container ) {
                    return;
                }
    
                const button = container.querySelector( 'button' );
    
                if ( ! button ) {
                    return;
                }
    
                const menu = container.querySelector( 'ul' );
    
                // Hide menu toggle button if menu is empty and return early.
                if ( ! menu ) {
                    button.style.display = 'none';
                    return;
                }
    
                button.setAttribute( 'aria-expanded', 'false' );
                menu.setAttribute( 'aria-expanded', 'false' );
                menu.classList.add( 'nav-menu' );
    
                button.addEventListener( 'click', function () {
                    container.classList.toggle( 'toggled' );
                    const expanded = container.classList.contains( 'toggled' )
                        ? 'true'
                        : 'false';
                    button.setAttribute( 'aria-expanded', expanded );
                    menu.setAttribute( 'aria-expanded', expanded );
                } );
    
    ...

    However, it works only after odd numbers of AJAX reloads; 1, 3, 5 etc. Maybe I need to unbind it before I bind it anew? How do I do that, since the function bound to the event is an anonymous function? I have tried to do it with jQuery, but the problem remains.

    Does anyone know how I can get the toggle to work after all my AJAX reloads?

    Regards,
    Almir

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Almir,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Feel free to get back to us in case you have any additional questions!

    Thread Starter almirdzankovic

    (@almirdzankovic)

    Hi.

    Thank you for the reply.

    I saw that I didn’t need the part of the AJAX that caused this requirement; and then I also removed that whole AJAX reload functionality, because it was causing an incompatibility with BeRocket’s AJAX Products Filter plugin. I still have some other AJAX functionality, but it doesn’t seem to cause any incompatibility.

    Regards,
    Almir

    Hi @almirdzankovic

    Thank you for sharing this here ?? and I am glad you were able to resolve this issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Mobile Site Navigation Menu Toggle Button Not Working After AJAX’ is closed to new replies.