Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

div click outside to hide javascript

$(document).click(function(){
 $("#try").hide(); 
});

$("#try").click(function(e){
  e.stopPropagation(); 
});
Comment

JavaScript hide element on click outside

function hideOnClickOutside(element) {
    const outsideClickListener = event => {
        if (!element.contains(event.target) && isVisible(element)) { // or use: event.target.closest(selector) === null
          element.style.display = 'none';
          removeClickListener();
        }
    }

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

    document.addEventListener('click', outsideClickListener);
}

const isVisible = elem => !!elem && !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length ); // source (2018-03-11): https://github.com/jquery/jquery/blob/master/src/css/hiddenVisibleSelectors.js
Comment

div click outside to hide javascript

<body>
  <p id="try">Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.

</p>
</body>
Comment

PREVIOUS NEXT
Code Example
Javascript :: js transform 
Javascript :: javascript delete first character in string 
Javascript :: javascript dedupe array 
Javascript :: how to create infinite loop in javascript 
Javascript :: axios download excel file 
Javascript :: clear arrays in jquery 
Javascript :: query select multiple classes 
Javascript :: form data append jquery 
Javascript :: cypress display timestamp in milliseconds 
Javascript :: javascript game loop 
Javascript :: js 2d array to object 
Javascript :: sql json_extract 
Javascript :: javascript how to know the end of the scroll 
Javascript :: async await catch error 
Javascript :: /on in jquery 
Javascript :: Use History React Router v6 app 
Javascript :: how to check if a string is alphabetic in javascript 
Javascript :: how to always run validators mongoose 
Javascript :: js canvas rectangel 
Javascript :: load script defer 
Javascript :: duplicate an array in javascript n times 
Javascript :: flatten in js without lodash 
Javascript :: window.onload in javascript 
Javascript :: remove appended element jquery 
Javascript :: javascript get child element by class 
Javascript :: javascript rtsp player 
Javascript :: jquery get URL slug 
Javascript :: google script wait 
Javascript :: speedtest-net node.js 
Javascript :: jquery visible 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =