jQuery Detect Click Outside of Element
jQuery Detect Click Outside of Element – You can detect the outside click of element using pure JavaScript or JQuery. There are many ways to detect the click outside of an element. Here in this tutorial we are going to explain how you can detect the outside click using jQuery. We will explain this functionality with example and demo so that we can understand it easily.
jQuery Detect Click Outside of Element Example
Here we have created an example will detect the click outside of a div(element).
Using jQuery
You can detect the outside click using the jquery as below –
jQuery Detect Click Outside of Element Example
<!DOCTYPE html> <html> <head> <title>jQuery Demo example</title> <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"> </script> <script type="text/javascript"> $(document).click(function (e) { if(!$(e.target).is('#myDiv')){ alert("Clicked Out Side"); } }); </script> </head> <body> <div id="myDiv" style="width:200px;height:200px;background:yellow;"> <h2>Click OutSide</h2> </div> </body> </html> |
If you run the above example it will produce output something like this –
Advertisements