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.

Discussion

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.

Example

mouseup

mouseup and mousedown

Sources: jQuery Official Guide and Stakeoverflow


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #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
PHP Variables ScopePHPIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousINTERSECT and EXCEPT in PostgresPostgresFile System Integrity: How to Keep an Eye on Your Files and Folder Change?MiscellaneousHow to make WordPress main stylesheet (style.css)?WordPressCSS backface-visibility PropertyCSSCheck If a String Contains a Specific Word in PHPPHPThe 8 Golden Rules of Programming I Have Learned in My CareerMiscellaneousHow to get ACF values from custom post type?WordPressHow to detect if browser supports WebP format on server side PHP script?PHPCurrencyPipe in Angular 9 with ExamplesAngular@forward rule in SCSS (Sass)SCSS