Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

Click Outside Close Menu Box Tutorial

In this JavaScript exercise we will demonstrate how to close specific things when a user clicks outside of them. You may have drop down menus that you want to close when a user interacts with other things on the page. Or perhaps you have information windows that appear over content and you want to close them if the user clicks outside of them. Closing multiple magic menus or boxes var boxArray = ['box1','box2','box3']; window.addEventListener('mouseup', function(event){ for(var i=0; i < boxArray.length; i++){ var box = document.getElementById(boxArray[i]); if(event.target != box && event.target.parentNode != box){ box.style.display = 'none'; } } }); Closing single elements window.addEventListener('mouseup', function(event){ var box = document.getElementById('box1'); if (event.target != box && event.target.parentNode != box){ box.style.display = 'none'; } }); Below is a link to a discussion about hierarchy of more deeply nested elements within the target box to close. http://webintersect.com/questions/2125/javascript-click-outside-close-box-issue
thumb

JavaScript Videos

General Programming

Flat Icons HTML Code Symbols Discovery JavaScriptHow to Program Code Syntax Highlighter Using JavaScriptFat Arrow Functions JavaScript Programming TutorialJavaScript Arguments Object Function TutorialJavaScript Storage Interface sessionStorage localStorage TutorialMulti Column Layout Grid Programming TutorialCustom Alert Box Programming TutorialCustom Confirm Box Programming TutorialCustom Prompt Box Programming TutorialChange CSS Class Style className Toggle TutorialPartial Print Document Tutorial HTML div ContentClick Outside Close Menu Box TutorialPercent Math Calculations Programming TutorialDual Image Reveal Control TutorialPage Loading Screen Document Preloader TutorialControl Panel Flyout Menu Windows AnimationsCustom Data Attributes HTML JavaScript CSS TutorialFullscreen API JavaScript Code Examples and SpecificationConsole Log Tutorial Chrome Firefox IE BrowsersToggle Function Click Drop Down Menus TutorialChange Tab Text and Window Title On The Fly TutorialDice Roll Programming Tutorial For Web Browser GamesDynamic Lorem Ipsum Placeholder Text TutorialBest HTML Event Handling addEventListener TutorialsetAttribute removeAttribute Methods TutorialDisabled User Handling Redirect WebsiteCapture Keyboard Input Tutorial HTML ApplicationCapture Keyboard Input Condition Logic TutorialChange Style Sheet Using Tutorial CSS Swap StylesheetWeb Page Overlay Tutorial Transparent CoverLights Out For Earth Day Tutorial Dark Page SwitchWindow Size Responsive CSS Layout Stylesheet Change TutorialRead Mouse Coordinates Pointer Position Tutorial