Yogesh Chauhan's Blog

How to hide a DIV on clicks outside of it using jQuery?

in jQuery on February 17, 2021

We can use jQuery mouseup() method to check the user click on the document.

This is how we can do that.

In the example above we check if the target of the click is neither the container nor a descendant of the container and if so, we will hide the element.


jQuery mouseup() method

This method binds an event handler to the “mouseup” JavaScript event. It triggers that event on an element specified.

This method is a shortcut for .on(‘mouseup’, handler) and .trigger(‘mouseup’).

The mouseup event is sent to an element when the mouse pointer is over the element, and the mouse button is released.



mouseup and mousedown

Sources: jQuery Official Guide and Stakeoverflow

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
A quick introduction to API, REST API and PostmanMiscellaneousWordPress: How to access first and random row values from a repeater field in ACF?WordPressHow to make flexbox items of the same size?CSSHow to define visibility for a property in PHP?PHPWhat is the difference between float and double?MiscellaneousHow to change HTML content on click using JavaScript?JavaScriptHow to create a flip pricing table using CSS and JavaScript?CSSHow to create a Progress Bar using JavaScript?JavaScriptReverse a String in JavaScriptJavaScriptWhat is XP (Extreme Programming) and When should it be used?MiscellaneousFULL OUTER JOIN in PostgresPostgresAngular 9 time clock update every minute, second, hourAngular