I try to close a dropdown menu and it's difficult for me. I manage to open the menu, to chose a currency and move the right label to the top of the div.
But how to close it by removing .expanded
if I click anywhere (outside the div, on a label, on the top label) ? I also have to reopen if I click on the container (by toggling .expanded
again)
var $container = $('.maincar__currency')
$container.click(function() {
$(this).addClass('expanded');
});
$(".maincar__currency label").click(function() {
$container.prepend($(this));
});
//$(document).click(function() {
// $container.removeClass('expanded');
//});
.maincar__currency {
display: flex;
flex-direction: column;
min-height: 32px;
max-height: 32px;
margin-left: auto;
margin-bottom: 10px;
overflow: hidden;
border-radius: 6px;
font-size: 14px;
}
input {
display: none
}
.maincar__currency label {
display: flex;
width: 65px;
height: 32px;
padding: 6px;
margin-right: 0 auto;
text-align: center;
cursor: pointer;
}
.maincar__currency label:first-child::after {
content: "▾";
margin-top: -1px;
font-size: 15px;
}
.expanded {
max-height: 132px;
position: relative;
right: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="maincar__currency" id="currency-drop-is">
<label for="euro-radio-is">
<div class="currency currency__euro">
<input type="radio" name="currency-is" value="euro" id="euro-radio-is" class="euro_radio_is" checked>
<span class="default">EUR</span>
</div>
</label>
<label for="dollar-radio-is">
<div class="currency currency__dollar">
<input type="radio" name="currency-is" id="dollar-radio-is" class="dollar_radio_is" value="dollar">
<span>USD</span>
</div>
</label>
</div>