By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – close dropdown for input-“search” on -webikit-search-cancel-button click
JavaScript

Javascript – close dropdown for input-“search” on -webikit-search-cancel-button click

Admin
Last updated: 2023/12/24 at 1:52 PM
Admin
Share
3 Min Read
close dropdown for input-"search" on -webikit-search-cancel-button click

Problem:

I have an input box with type=”search” and below that a list of options that shows in a dropdown when the input box is clicked. I managed to get this to work.
However the issue I run into now: whenever the ‘X’ in the input box (the search-cancel-button) is clicked the input text in the input box is deleted but the dropdown doesn’t disappear.
How could I fix this?

Contents
Problem:Solution:

Below is my code:

.form .formtextbox {
  position: relative;
  display: inline-block;
  width: 100%;
}

.form .formtextbox .options {
  display: none;
  position: absolute;
  background-color: var(--white);
  min-width: 100%;
  box-shadow: 0px 2px 7px 0px #0000001F;
  padding: 12px 16px;
  z-index: 1;
}

.form .formtextbox:focus-within .options {
  display: block;
}
<form method="get" action="">
  <fieldset class="form">
    <div class="formtextbox">
      <input title="search_form_title" type="search" placeholder="Search" autocomplete="off">
      <div class="options">
        <ul unselectable="on">
          <li>apple</li>
          <li>banana</li>
          <li>pineapple</li>
          <li>strawberry</li>
        </ul>
      </div>
    </div>
    <div class="formbutton">
      <input type="submit" hidden />
    </div>
  </fieldset>
</form>

Solution:

One way to do this is to use a little JavaScript. What I have done is added a function which listens for the X button click event and then hides the dropdown options. The X button does not dispatch the click event, but rather an input event. We can listen for the same and hide the dropdown options when it occurs. Additionally, I have added ids to input and options element to refer them in Javascript. You can use follow other ways well. Something like:

document.getElementById('search-input').addEventListener('input', function() {
  var options = document.getElementById('options-container');
  if (this.value.length === 0) {
    options.style.display = 'none';
  } else {
    options.style.display = 'block';
  }
});
.form .formtextbox {
  position: relative;
  display: inline-block;
  width: 100%;
}

.form .formtextbox .options {
  display: none;
  position: absolute;
  background-color: var(--white);
  min-width: 100%;
  box-shadow: 0px 2px 7px 0px #0000001F;
  padding: 12px 16px;
  z-index: 1;
}

.form .formtextbox:focus-within .options {
  display: block;
}
<form method="get" action="">
  <fieldset class="form">
    <div class="formtextbox">
      <input id="search-input" title="search_form_title" type="search" placeholder="Search" autocomplete="off">
      <div id="options-container" class="options">
        <ul unselectable="on">
          <li>apple</li>
          <li>banana</li>
          <li>pineapple</li>
          <li>strawberry</li>
        </ul>
      </div>
    </div>
    <div class="formbutton">
      <input type="submit" hidden />
    </div>
  </fieldset>
</form>

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article how to only submit data change in react hook form Javascript – how to only submit data change in react hook form
Next Article Why does my click event require two clicks (react) Javascript – Why does my click event require two clicks (react)
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?