I have a table that has a dropdown button on each row. When you click on the button, a drop-down list opens, when you click outside the area, the drop-down list is closed. How to make sure that when clicked in the drop-down list area, it does not close? And how to make sure that when you click on another button with a drop-down list, the previous drop-down list is closed?
$(".dropdown").click(function(e) {
$(this).find(".dropdown-menu").slideToggle();
});
$(document).on("click", function(event) {
var $trigger = $(".dropdown");
if ($trigger !== event.target && !$trigger.has(event.target).length) {
$(".dropdown-menu").slideUp("fast");
}
});
.dropdown {
display: inline-block;
position: relative;
}
.dropdown-menu {
text-align: center;
font-size: 14px;
background-color: #ffffff;
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td>
<li class="dropdown">
<button></button>
<ul class="dropdown-menu">
<li></li>
<li></li>
</ul>
</li>
</td>