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.
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.
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.
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.
Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.
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.
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.
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 () });
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.
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>
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'/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With