LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Capture The Mouse Click On DIV and Outside The DIV On A Web Page

Capture The Mouse Click On DIV and Outside The DIV On A Web Page

Capture-The-Mouse-Click-On-DIV-and-Outside-The-DIV-On-A-Web-Page
Do you want to create a popup when a button is clicked and close when anywhere else rather than a popup is clicked in vanilla JavaScript? So here we also wanted to create a way where we can make the popup close when a user clicks outside the popup but keep the popup visible when clicked inside. Is that possible?

As you know It’s a reasonable UX thing that you can click-to-open something, and then not only be able to click that same thing to close it but click outside the thing that it opened to close it. The trick is that once the thing is opened, you attach an event handler to the window that watches for events (like another click). If that subsequent click did not happen within the newly-opened area, close it. Like, literally thing.contains(event.target). It’s a nice trick, I think.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Pure JavaScript Based Multi-Tab With Hash DIV Linkable URL

Features:

  1. Light Weight.
  2. Pure JavaScript Code.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

How To Capture The Mouse Click On DIV and Outside The DIV On A Web Page?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

HTML:

<div id="wrapper">
<center>
<div id="except"></div>
</center>
</div>

CSS:

<style type="text/css">
#wrapper {
background-color: blue;
height: 250px;
padding-top: 20px;
}
#except {
background-color: pink;
height: 100px;
width: 80%;
}
</style>

JavaScript:

<script type="text/javascript">
var body = document.getElementById('wrapper');
var except = document.getElementById('except');
body.addEventListener("click", function () {
alert("wrapper");
}, false);
except.addEventListener("click", function (ev) {
alert("except");
ev.stopPropagation();
}, false);
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
How To Show Black Opacity On Image Hover Through CSS?

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *