I'm trying to create the exact same button (visually dropdown) "price range", like the one in this website: https://dubai.dubizzle.com/en/property-for-rent/residential/apartmentflat/
/* CSS */
.button_wrapper {
background: white none repeat scroll 0% 0%;
border-radius: 6px;
height: 48px;
}
button {
display: flex;
-moz-box-pack: justify;
justify-content: space-between;
-moz-box-align: center;
align-items: center;
height: 100%;
color: black;
font-size: 14px;
box-sizing: border-box;
padding: 0px 6px 0px 12px;
border: 0px none;
width: 100%;
background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
text-align: left;
cursor: pointer;
}
.lnhPTn {
position: absolute;
background: white none repeat scroll 0% 0%;
color: rgb(182, 184, 185);
margin-top: 4px;
box-sizing: border-box;
border-radius: 6px;
border: 1px solid rgb(43, 45, 46);
height: auto;
padding: 16px;
}
.kFCjgJ {
display: flex;
align-items: flex-end;
-moz-box-pack: center;
justify-content: center;
}
.gacQrk {
font-size: 14px;
font-weight: 600;
line-height: 1.43;
color: rgb(43, 45, 46);
}
.fmQggb {
display: block;
width: 180px;
border-radius: 6px;
border: 1px solid rgb(182, 184, 185);
height: 48px;
padding: 8px 12px;
box-sizing: border-box;
font-size: 14px;
box-shadow: none;
}
..jaNqhG {
border: 0px none;
width: 8px;
margin: 0px 6px 18px;
height: 1px;
background: rgb(43, 45, 46) none repeat scroll 0% 0%;
}
<!-- HTML -->
<!-- BUTTON STARTS HERE -->
<div class="button_wrapper">
<button type="button" class="button_price">
<span class="button_span">Any</span>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M6 9l6 6 6-6"></path>
</svg>
</button>
<div class="lnhPTn">
<div class="bkwgFh">
<div class="kFCjgJ">
<label class="gacQrk" for="min">
Min
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="min">
</label>
<span class="jaNqhG"></span>
<label class="gacQrk" for="max">
Max
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="max">
</label>
</div>
</div>
</div>
</div>
<!-- BUTTON ENDS HERE -->
How to show the div below the button (the div which contain the inputs) only when clicking/toggling the button and automatically close when something else is clicked/toggled (like for instance the next button or search)?
Simplest solution in 3 step-
onclick="document.querySelector('.lnhPTn').style.display=document.querySelector('.lnhPTn').style.display=='block'?'none':'block'"
attribute on your button.<script>
window.onclick=function (e){
var buttonPrice =document.querySelector(".button_price");
var mainDiv=document.querySelector(".lnhPTn");
var inputs=document.querySelectorAll(".lnhPTn input")
console.log(inputs.length)
if(e.target!=buttonPrice && e.target!=mainDiv && e.target!=inputs[0] && e.target!=inputs[1]){
mainDiv.style.display="none";
}
}
</script>
at end of body tag.
So your final code will look like
button_wrapper {
background: white none repeat scroll 0% 0%;
border-radius: 6px;
height: 48px;
}
button {
display: flex;
-moz-box-pack: justify;
justify-content: space-between;
-moz-box-align: center;
align-items: center;
height: 100%;
color: black;
font-size: 14px;
box-sizing: border-box;
padding: 0px 6px 0px 12px;
border: 0px none;
width: 100%;
background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;
text-align: left;
cursor: pointer;
}
.lnhPTn {
display:none;
position: absolute;
background: white none repeat scroll 0% 0%;
color: rgb(182, 184, 185);
margin-top: 4px;
box-sizing: border-box;
border-radius: 6px;
border: 1px solid rgb(43, 45, 46);
height: auto;
padding: 16px;
}
.kFCjgJ {
display: flex;
align-items: flex-end;
-moz-box-pack: center;
justify-content: center;
}
.gacQrk {
font-size: 14px;
font-weight: 600;
line-height: 1.43;
color: rgb(43, 45, 46);
}
.fmQggb {
display: block;
width: 180px;
border-radius: 6px;
border: 1px solid rgb(182, 184, 185);
height: 48px;
padding: 8px 12px;
box-sizing: border-box;
font-size: 14px;
box-shadow: none;
}
..jaNqhG {
border: 0px none;
width: 8px;
margin: 0px 6px 18px;
height: 1px;
background: rgb(43, 45, 46) none repeat scroll 0% 0%;
}
<!-- HTML -->
<!-- BUTTON STARTS HERE -->
<div class="button_wrapper">
<button type="button" class="button_price" onclick="document.querySelector('.lnhPTn').style.display=document.querySelector('.lnhPTn').style.display=='block'?'none':'block'">
<span class="button_span">Any</span>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M6 9l6 6 6-6"></path>
</svg>
</button>
<div class="lnhPTn">
<div class="bkwgFh">
<div class="kFCjgJ">
<label class="gacQrk" for="min">
Min
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="min">
</label>
<span class="jaNqhG"></span>
<label class="gacQrk" for="max">
Max
<input class="fmQggb" type="number" step="1000" min="0" max="100000000" id="max">
</label>
</div>
</div>
</div>
</div>
<!-- BUTTON ENDS HERE -->
<script>
window.onclick=function (e){
var buttonPrice =document.querySelector(".button_price");
var mainDiv=document.querySelector(".lnhPTn");
var inputs=document.querySelectorAll(".lnhPTn input")
if(e.target!=buttonPrice && e.target!=mainDiv && e.target!=inputs[0] && e.target!=inputs[1]){
mainDiv.style.display="none";
}
}
</script>
Here's a working jsfiddle with this code https://jsfiddle.net/o48ps293/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments