Skip to main content

Click Outside Element To Close (Click On Body To Close)

By September 9, 2017Blog, ES6, Javascript
Allure Web Solutions Code Snippet

With this snippet you can click outside an element specified by a selector to perform some action. A common usage would be to close a popup when clicking outside of the popup.

function hideOnClickOutside(selector) {
  const outsideClickListener = (event) => {
    if (!$( {
      if ($(selector).is(':visible')) {

  const removeClickListener = () => {
    document.removeEventListener('click', outsideClickListener)

  document.addEventListener('click', outsideClickListener)

// Credit:



Author allure

More posts by allure

Leave a Reply

Designed by

best down free | web phu nu so | toc dep 2017