Close your Pop-Up Menu on Link Click

For those using same page links.

Read first: readers should be aware of the SEO consequences of using an Elementor Pop Up as your core navigation.

To begin with, insert an HTML element in the same popup as your menu

This will make Elementor close popup on click. After adding the code.

Elementor Close PopUp on Click for Menu & Same Page Links

Then, copy the code below in it

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

Finally, verify that it is working fine

That's it! Short and simple! Thanks for reading!

87 Responses

  1. Definitely... live sarver! Would you please help me getting it to close only on specific buttom click.
    I´ve modified it by adding the Button Name "yes" before click for it to close only on this specific button click and do something else on the second one.

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

    Thanks in advance for all your help!

    1. Hey Juan! your code attempt won't work, as 'click' is the event listener, not the selector. $(document) is what need to be replaced.

      Give your button the CSS class 'yesclick' , the use that code:

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

    1. You didn't copy paste the code properly. You have all kind of spaces in there, where they should not be, and that's why it's not working... rewrite it like this precisely, no spaces:

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

      1. Use the Elementor html element (with the icon) , not the one with the WordPress icon

      2. Hey man, I need your help haha. I created a popup menu and when I use your code, everything works fine, but when I have one more popup like WhatsApp icon where to open a other menu, when I click on it icon, its close.

        Well, How can I close a specific Section on elementor using your code?

      3. Good question!

        Here is how, simply change this line here :

        elementorProFrontend.modules.popup.closePopup( {}, event);

        to

        elementorProFrontend.modules.popup.closePopup( {id:498}, event);

        Then, change '498' to the actual ID of your popup. You can find it by looking at the URL when you are in the Elementor editor.

  2. Thanks for this! I've tried doing this, however after adding my pop-up still doesn't close and infact the close button/clicking out of overlay doesn't close the pop-up.

    Would you have any ideas as to why it's not working? It's the first link on the pop-up and I have this showing on mobile only

    staging3.entertainairs.com

  3. Hey! This seems simple enough, but I'm struggling a bit to find where I need to add the HTML element. Would appreciate your help 🙂

    Thanks in advance!

  4. I am trying to get this to work but nothing happens. I have a button on a page which loads an elementor popup with a form. When the user submits the form it loads a thank you popup to say I have received the form. The user can then close that popup with a click but the original popup is still there. I put this code into an HTML widget on the original popup template but when I click on the form submit button nothing happens to close the popup.
    Can anyone help as its driving me nuts!

  5. hello dear guys

    how can i use this code for a specific popup? the problem is this code close all the popups on the page .... ?

    i am not god at programing, can someone help me please ?

  6. Hi Bro, It was a headache, I was having a problem with elementor pro popup so I switched to jet-popup. The problem is this js code is not working with my popup. Can you give me a new piece of code? This popup is located in the article table of contents button. I want when I click on any anchor area on the popup it will jump to that anchor and disappear the popup. Sorry for using google translate, because I'm not good at this language, I'm from Vietnam. Thank you very much.

    Link : https://bit.ly/3yFw69k

  7. first, thanks for the code! is it possible to close the window after click AND cursor outside / "hover:not" - this would be very nice at a onepager. because you can step along the page in one flow and than it's closing automatically 8)

    1. Yep that's possible. Be aware of the SEO consequences of using a popup for your navigation though : https://element.how/why-elementor-pro-popups-are-terrible-for-seo/

      Otherwise, for what you want, this code should work :

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

  8. Great piece of code, thank you very much! It works great!

    One question - when I click any other links (not in main nav bar) in the popup, it still closes it. How can I avoid that?

    All the best!

    1. Hey Matej!

      In the code, you will see '.elementor-location-popup a'

      Change that CSS selector to match only the URLs you want to close the popup when clicked on!

    1. Hey Andi!

      Yes it's possible to have it close... add

      $('.hamburger').eq(0).click();

      after

      elementorProFrontend.modules.popup.closePopup( {}, event);

      in the JS and it should work!

      1. Didn't work for me, but I found the issue:

        Like Andi above I also have use your animated icon tutorial to create a beautiful popup menu (and yes I'm aware of the consequences, hehe 😊).

        As Andi also asked the code for this tutorial only closes the popup, but doesn't trigger the icon before closing the pop-up, which results in the icon staying as a "X". (or whatever active state icon you use)

        I had to add the class ".is-active" to your snippet for it to work, so the code will be:

        jQuery(function(jQuery){
        jQuery(document).on('click','.elementor-location-popup a', function(event){
        elementorProFrontend.modules.popup.closePopup( {}, event);
        jQuery('.hamburger.is-active').eq(0).click();
        });
        });

  9. Aint working for me unfortunately.. dno why.
    I've added the html widget in the same column as the navigation widget on mobile view. The menu calls for an anchor on the site but doesnt close after the click.
    Any ideas?
    ¨
    (mobileview)
    https://hatchlingznft.com/

  10. @Maxime I also discovered that if you use Prevent Page Scrolling on your popup and just close the popup with your code, then the css hangs onto the , which prevents the website from scrolling anywhere unless refreshing.

    I assume that Elementor doesn't recognise the custom jQuery click function, so it keeps the code... what can be done to fix this issue?

    This code is the problem:

    .dialog-prevent-scroll {
    overflow: hidden;
    max-height: 100vh;
    }

  11. This works great thank you so much!

    Is it possible to identify a specific ID of a popup to close? I have a popup opening a popup, but want that first popup to not close when a link is clicked in the second popup (second popup is one page menu on mobile, first popup is bottom sticky button to open up second popup).

    1. Hey Tie!

      Good question!

      Here is how, simply change this line here :

      elementorProFrontend.modules.popup.closePopup( {}, event);

      to

      elementorProFrontend.modules.popup.closePopup( {id:498}, event);

      Then, change '498' to the actual ID of your popup. You can find it by looking at the URL when you are in the Elementor editor.

  12. hey how to close the pop in one click, i mean i have to click it twice for it to be closed, any idea?

    i'm using premium button from premium addons

  13. I created a popup on the "Simple Motion Switches" images. I want the popup to close after I click "Select Your Package" because I've attached an anchor link to it. But it won't close

      1. I just checked and your code is different... and it's not in every popup...

      2. Hey Maxime! Ya it's only in the popup that appears when you click the "Simple Motion Switch" image

    1. Hey Ester!

      Welcome!

      And here is how... Change the similar line of code, to this :

      elementorProFrontend.modules.popup.closePopup( {id: yourPopupIdHere}, event );

      Cheers!

  14. Im using an extra plugin for users to select product categories. The script doesn't work on the button generated by the plugin. I've tried adding other classes than the ".elementor-location-popup a" in Maxime's example.

    Would it be possible for you to look at the site and provide the solution? 🙂
    Site adress: https://kunstvanmetaal.nl/winkel/
    Side note: Popup only activated for Tablet and mobile
    Tap the "Categorieën" button to open the popup

    1. Hey Michel!

      You don't have the proper code in there... you have this:

      window.addEventListener('DOMContentLoaded', function() {
      jQuery(function($){
      $(document).on('click','.elementor-location-popup a', function(event){
      elementorProFrontend.modules.popup.closePopup( {}, event);
      $('.hamburger').removeClass('is-active');
      });
      });
      });

      It should be

      jQuery(function($){
      $(document).on('click','.elementor-location-popup a', function(event){
      elementorProFrontend.modules.popup.closePopup( {}, event);
      $('.hamburger').removeClass('is-active');
      });
      });

      And also, exclude it from WP Rocket delay JS by adding ".hamburger" there.

      Cheers!

  15. Hey!
    I wanted to ask, is it possible to get the popup to close when clicking on a certain menu item.
    For instance I changed the 'document' value to a certain Css id, because the code was opening the popup even when I clicked contact form button.
    So right now, the issue is gone, only the right popup opens, but after clicking the menu item it does not close. I added the css id to all the menu-items, but still, it is not closing.

    The code is following.
    script>
    jQuery(function($){
    $('#yesclick').on('click','.elementor-location-popup a', function(event){
    elementorProFrontend.modules.popup.closePopup( {}, event);
    });
    $('.elementor-location-popup').mouseleave(function(){
    elementorProFrontend.modules.popup.closePopup( {}, event);
    });
    });

    1. So basically, I have multiple popups. Contact popup and menu popup. They are defined by button click and a certain popup opens, but if im using your code above, when going first to the page, and clicking I accept on cookies bar button, the menu popup, where the code is also opens...
      Hope you get the idea - is it somehow changable?

    2. Do you really want just want it to close after clicking the menu item? How is that popup opened to start with?

      Also, what you describe is from your popup conditions... if you click the "accept cookie" button and your popup opens, this has nothing to do with the code I provide in this tutorial. That code only closes popup, there is nowhere where it would trigger one open.

      The problem is in your conditions, under "triggers", you probably added "click". Remove this.

      1. Right, so right now the popup opens via the elementor link (dynamic tag - popup open).

        Yeah, I want to close the popup on menu click, because I have a one-pager that will anchor to a certain section after the click, therefore I need the popup to close after the click on menu item.

        Thanks in advance! 🙂

  16. The code you shared, simply won't work. You can't add a CSS ID to more than one item without generating invalid HTML.

    Even if you could, that modified JS you have won't work. Use the code I provide in the tutorial and follow each step, it will work as expected, as it did for so many others.

Leave a Reply