How to create a button that shows some inputs only when clicked/toggled?

SkylerX

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/

This button: enter image description here

/* 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)?

Factouch

Simplest solution in 3 step-

  1. Add onclick="document.querySelector('.lnhPTn').style.display=document.querySelector('.lnhPTn').style.display=='block'?'none':'block'" attribute on your button.
  2. Add
<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.

  1. In css set main div's display to none.

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to make submit button only submit when the total of inputs combined is equal to or less than X

How to take inputs from many widgets but only recalculate when submit button is pressed?

Want to create an app in which it shows 3 button vertically and horizontally at the center but when I have tried only the 3rd button was there ,

SwiftUI: Enable save button only when the inputs are not empty

Close/hide a div only when the inputs are complete, without a submit button

How to build a shiny app that shows the output ONLY when the user clicks a click button?

Creating a new room (including inputs and button) and only template shows without the values inside (rcc)

Vanilla JavaScript: How to dynamically create a button that shows up after user has selected some text and then do something with that text?

How to disable button when stage shows

How to validate number in inputs with javascript when pressing button?

Xlsxwriter doesn't create file upon some inputs from a dataframe, only spits gibberish into terminal

Soft keyboard shows a "Go" button instead of a "Search" button when entering some text in a SearchView widget

How to validate 2 inputs in Powershell and only proceed when both inputs are validated

Copy function only working on some inputs

Box and input inline in Shiny, but only for some inputs

WordPress widget settings saving only some inputs

How to create a button using only JS and HTML

OpeneERP: How to create a button with no icon (only text on it)

How to make the WPF app to, open only one new window or create only single instance of the XAML, when clicking button in MainWindow?

How can I set the width of a row of multiple inputs to a percentage of the browser window width when some inputs are of fixed size?

Android: How to set activity that shows only once (when app is installed)

How to create and animate an uneven hamburger button into an X button with CSS only?

Some inputs disabled when checked option

how to display a div only when a button is clicked

How to send only inputs with value?

Timedelta only shows 2 digits in some cases

How to create a card hero slider that when clicked shows/hides cards?

while loop shows only the last row in database when click at button[to display modal dialog]

when i click on button to showing next activity it shows only white screen