Thanks to SO posts I'm at last able to close a div by clicking anywhere outside it. My problem is that my div to be closed contains additional divs with checkboxes. I want to be able to click the checkboxes without closing the div. Does anyone know the most efficient way of doing this? I think the fiddle demonstrates the problem http://jsfiddle.net/s72q85fm/ Any help much appreciated.
$(function() {
$("#startbutton").click(function(e) {
if ($('#MainContainer').is(":visible")) {
$('#MainContainer').fadeOut(200);
} else {
e.preventDefault();
$("#MainContainer").fadeIn(200, function() {
$(this).focus();
});
}
});
$("#MainContainer").on('blur', function() {
$(this).fadeOut(200);
});
});
#MainContainer {
width: 200px;
height: 200px;
background: green;
color: #FFFFFF;
display: none;
position: absolute;
left: -30px;
}
#CheckboxContainer {
width: 100px;
height: 100px;
background: yellow;
position: absolute;
left: 60px;
margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Universe">
<button id='startbutton'>Open Box</button>
<div id="MainContainer" tabindex="-1">
<div id="CheckboxContainer">
<label>
<input type="checkbox" id="myRegion" />
</label>
<label>
<input type="checkbox" id="myCountry" />
</label>
</div>"
</div>
</div>