WP responsive navbar auto close

33 views Asked by At

Im trying to make my navbar auto close when I click on any link in my navbar. I found the next script but it wont work. Im pretty sure im using it wrong, so i'll be happy if you guys can help me to figure it out: ``

<script>
jQuery(function($){
$(document).on('click','.elementor-location-popup a', function(event){
elementorProFrontend.modules.popup.closePopup( {}, event);
});
});
</script>
``

I couldnt find any post about it here in StackOverflow, so if there's a good one i'll be more than happy to have it.

Thank you!

1

There are 1 answers

0
Frizzant On

I think this could get you in the right direction.

closeIfAnyLinkIsClicked() {
        const parents = document.querySelectorAll('.elementor-location-popup')

        for (let parent of parents) {
            const links = parent.querySelectorAll('a')

            for (let link of links) {
                link.addEventListener('click', () => {
                    /** reverse whatever opened your navigation */
                    
                    /** remove the added class on your navigation */
                    parent.classList.remove('nav-is-open')
                    /** using a checkbox, you'd be doing something like this */
                    parent.checked = false
                })
            }
        }
    }
    
 closeIfAnyLinkIsClicked()