HTML
<div id="selectPeriodRangePanel">
<p>Test</p>
</div>
<button id="period_select_range_btn">Select Range</button>
JQUERY
$("#period_select_range_btn").off().on("click", function(){
$("#selectPeriodRangePanel").toggle();
});
When i click on button, the selectPeriodRangePanel DIV
is opened. I would like to hide this DIV
when i click outside of it.
How can i do that ?
Select the div element. Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.
To set an onClick listener on a div element in React:Set the onClick prop on the div. The function you pass to the prop will get called every time the div is clicked. You can access the div as event. currentTarget .
Try this
$(document).on("click", function(e){
if($(e.target).is("#period_select_range_btn")){
$("#selectPeriodRangePanel").show();
}else{
$("#selectPeriodRangePanel").hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="selectPeriodRangePanel" style="display:none">
<p>Test</p>
</div>
<button id="period_select_range_btn">Select Range</button>
You can have a click handler for the document element, where if the click has happened outside then you can hide it.
$("#period_select_range_btn").off().on("click", function() {
$("#selectPeriodRangePanel").toggle();
});
$(document).click(function(e) {
if ($(e.target).closest('#selectPeriodRangePanel, #period_select_range_btn').length == 0) {
$("#selectPeriodRangePanel").hide();
}
})
body {
min-height: 500px;
background-color: lightblue;
}
#selectPeriodRangePanel {
background-color: lightgrey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="selectPeriodRangePanel">
<p>Test</p>
</div>
<button id="period_select_range_btn">Select Range</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With