How to close the popup when clicked outside the element

Posted By : Vinay Tiwari | 02-Jun-2017

This blog explains the solution to resolve the problem of closing the popup when any outer element is clicked. I have used jquery function to solve it.

This function explain us, how to target every element of popup and differntiating it with the outer elements. If function gets the element in which we have targeted, then it closes the popup.

 

Now we will define the code structure

 

HTML 

	

Spotter Finance is Waiting find Quotes for you

 

Important Information

 

CSS 

.quote-info-bg {
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 99;
    height: 100%;
    overflow-y: scroll;
    left: 0px;
}
.quote-popup {
    background: #fff;
    padding: 65px;
    position: absolute;
    margin: 10px auto 10px;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 804px;
    border-radius: 10px;
}
	

 

Jquery 

$(".quote-info-bg").click(function(event ){
	var $target = $(event.target); 
	if($target.hasClass("quote-info-bg")){
		$(".quote-info-bg").hide();
	}
});

 

Declaration 

 

The event.target attribute can be registered as an element for a particular event or as a descendant of the event in consideration.  Event.target is frequently compared to this to decide whether the event is handled because of the concept event bubbling. In Event delegation 'this' property is very useful, especially when events bubbling occurs.

 

Thanks

 

About Author

Author Image
Vinay Tiwari

Vinay is a bright UI developer, having knowledge of HTML, CSS, Bootstrap, Jquery and AngularJs. His hobbies are interacting with people, listening music etc.

Request for Proposal

Name is required

Comment is required

Sending message..