Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear datalist input on click?

I have this datalist inside a Lit-Element web component:

<input list="cars" name="car" placeholder = "Type car name">

<datalist id="cars">
  <option value="Jeep">
  <option value="Lamborghini">
  <option value="Ferrari">
  <option value="Fiat 500">
  <option value="Gran Torino">
</datalist>

If I select one of these options from the list, and then I click again on input field, I can't see the list of option but only the selected one. If I want to see all the options I have to manually delete with the keyboard the option written on input field.

Does it exist a way to show all the options even if one of these options is selected?

Or better, does it exist a way to clear the input field on focus or on click?

Without JQuery.

like image 655
margot_eddie Avatar asked Sep 01 '20 09:09

margot_eddie


People also ask

What is difference between Datalist and select?

Select input element presents options for the users from which they need to select one of them. On the otherhand, Datalist presents a list of suggested values to the associated input form (text) field and users are free to select one of those suggested values or type in their own value.

Is the Datalist tag and select Tag same in HTML?

Generally, both the tags are used for choosing an option from the given list. But the main difference between both is that in the <datalist> tag the user can enter its own input and add that as an option with the help of the <input> element whereas the <select> tag doesn't provide this feature.

What does Datalist mean in HTML?

Definition and Usage The <datalist> tag specifies a list of pre-defined options for an <input> element. The <datalist> tag is used to provide an "autocomplete" feature for <input> elements. Users will see a drop-down list of pre-defined options as they input data.

Is Datalist supported by all browsers?

Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.

How to clear input fields on button click in JavaScript?

In this tutorial, you will learn how to clear input fields on button click in javascript. Whenever you create a form whether it’s for login or signup, you must provide your visitor the ability to reset the form, which eventually will clear all input fields. If you are using the form element, then you can use the reset () method.

Is there a way to clear the input field on focus?

Or better, does it exist a way to clear the input field on focus or on click? Without JQuery. Create onfocus and onchange listeners on your input element to clear focus after selecting an option, then clear input on focus.

Is it possible to clear a field by clicking on it?

Yes you can clear a field by clicking on it but I'd recommend to only select it on click and not delete it directly since a miss click could happen. YOUR_ELEMENT.addEventListener ('click', mouse_event => { mouse_event.target.select () });

How to clear the input field in WordPress?

Therefore, in this article, we are going to learn about how to clear the input field. There are two ways to do this and both are super easy and short. Let us check out both of them. Method 1: Clearing Input on Focus. Create an input field. Method 2: Clearing Input with the help of button. Create a button. Get the id of input field.


2 Answers

Create onfocus and onchange listeners on your input element to clear focus after selecting an option, then clear input on focus.

<input list="cars" name="car" onfocus="this.value=''" onchange="this.blur();" placeholder = "Type car name">

<datalist id="cars">
  <option value="Jeep">
  <option value="Lamborghini">
  <option value="Ferrari">
  <option value="Fiat 500">
  <option value="Gran Torino">
</datalist>
like image 143
Alfred Avatar answered Oct 17 '22 23:10

Alfred


Yes you can clear a field by clicking on it but I'd recommend to only select it on click and not delete it directly since a miss click could happen.

    YOUR_ELEMENT.addEventListener('click', mouse_event =>{
        mouse_event.target.select() 
    });
    YOUR_ELEMENT.addEventListener('click', mouse_event =>{
        mouse_event.target.value = '' 
    });

check the snippet

document.getElementById('test').addEventListener('click', (e) => {
        e.target.value = ''
})
document.getElementById('test2').addEventListener('click', (e) => {
        e.target.select()
})
<input type='text' id='test' placeholder='delete'/>
<input type='text' id='test2' placeholder='select'/>
like image 1
Axel Moriceau Avatar answered Oct 17 '22 23:10

Axel Moriceau