How to hide the list when clicking outside the element?

Hi!
there is a list with jquery
How to hide it when clicking outside the element?

tried it before, but also opens up when you click outside the element either immediately closes when clicking on an item

$(document).click(function (e) {
$('.select_inner').slideToggle('fast');
 return false;
 });
March 12th 20 at 08:01
1 answer
March 12th 20 at 08:03
Solution
Good evening.
Try this
<div id="block" style="border: 1px solid; height: 100px; width: 100px"></div>
<script type="text/javascript">
jQuery(function($){
 $(document).mouseup(function (e){ // track the event of clicking on the web document
 var block = $("#block"); // define the element to which we apply conditions (can specify ID, class or any other item ID)
 if (!block.is(e.target) // check if click was not on our block
 && block.has(e.target).length === 0) { // check if click is not on its child elements
 block.hide(); // if conditions are met hide our element
}
});
});
 </script>


p.s. everything works
thanks for the reply, but the list is no longer open
https://jsfiddle.net/Jonatan/afhewzum/1/ - Jonatan commented on March 12th 20 at 08:06
@Jonatan, Your trouble, and not only Yours, that You are mindlessly copying code You show as an example.
$(document).mouseup(function (e){ // track the event of clicking on the web document
 var block = $(".select_wrapper"); // define the element to which we apply conditions (can specify ID, class or any other item ID)
 if (!block.is(e.target) // check if click was not on our block
 && block.has(e.target).length === 0) { // check if click is not on its child elements
 block.find('ul').hide(); // if conditions are met hide our element
}
 });
- Myrna.Senger commented on March 12th 20 at 08:09
@Jonatanand don't forget to remove the comment code, and then you say again that You have something does not work there.
$('select').parent().on('click', function (){
$(this).find('ul').slideToggle('fast');
 });
- Myrna.Senger commented on March 12th 20 at 08:12
@Myrna.Senger, a floor of day tried to win zamylilsya eyes =) thanks, corrected - Jonatan commented on March 12th 20 at 08:15
@Jonatan, since work, then celebrate the decision. - Myrna.Senger commented on March 12th 20 at 08:18

Find more questions by tags jQueryJavaScript