I make a JavaScript driven html popup box . The code work perfectly for me ,but now I need to press the close link to close the popup box. How can I modify the below code so that I can also close the popup box by clicking outside the popup box.
function toggle_visibility(id) {
var e = document.getElementById(id);
if (e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
#popupBoxOnePosition {
top: 0;
left: 0;
position: fixed;
width: 100%;
height: 120%;
background-color: rgba(0, 0, 0, 0.7);
display: none;
}
.popupBoxWrapper {
width: 550px;
margin: 50px auto;
text-align: left;
}
.popupBoxContent {
background-color: #FFF;
padding: 15px;
}
<div id="popupBoxOnePosition">
<div class="popupBoxWrapper">
<div class="popupBoxContent">
<h3>Popup Box 1</h3>
<p>You are currently viewing popup box 1.</p>
<p>Click <a href="javascript:void(0)" onclick="toggle_visibility('popupBoxOnePosition');">here</a> to close popup box one.</p>
</div>
</div>
</div>
<p>Click <a href="javascript:void(0)" onclick="toggle_visibility('popupBoxOnePosition');">here</a> to see popup box one.</p>
Please note I try below code but it doesn't work for me
$('#popupBoxOnePosition').click(function(event){
event.stopPropagation();
});