Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Hide a Div Using JavaScript on Outside Click



To hide a div when the user clicks outside of it, try to run the following code

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         window.onload = function(){
            var hideMe = document.getElementById('hideMe');
            document.onclick = function(e){
               if(e.target.id !== 'hideMe'){
                  hideMe.style.display = 'none';
               }
            };
         };
      </script>
      <div id="hideMe">Click outside this div and hide it.</div>
   </body>
</html>
Updated on: 2019-09-18T08:30:50+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements