jQuery Click Outside DIV

CyberBotMachines
1 min readSep 10, 2021

--

Learn how to detect that a click was made outside a specific div element using jQuery.

jQuery click outside div

TL;DR Summary

Detecting a click outside div element using jQuery is fairly easy. All you need is two things:
1) set a click listener on some higher element in the structure (e.g. body)
2) a way to uniquely identify the div

You can do that either through its ID:
event.target.id
or using its class name:
event.target.className
or using some of the element’s attributes:
$(event.target).attr('hello')

Below is a full code example for the case with the ID (but you can easily substitute that line of code with any of the mentioned above)

And that’s all there is to detecting a click outside a div using jQuery.

Until next time,
Will
Senior Developer & SEO Algo Expert

==> FYI — if you want to build a startup with high success rate — then learn how to get FREE web traffic to your website. Because without web traffic you don’t have a startup — you have a dream.

--

--