How to hide a datalist's options using CSS - html

Given the following code:
<label for="myBrowser">Choose a browser from this list:</label>
<input list="browsers" id="myBrowser" name="myBrowser" />
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
<option value="Microsoft Edge">
</datalist>
The following UI is rendered:
What CSS can be added without changing the HTML and without using JavaScript to take away the auto-completed items so it behaves like a datalist-less input?
The following UI is desired:

It is not solved with css, but try use autocomplete="off" in your input

Related

FireFox datalist does not open until double click

<input list="browsers" name="myBrowser" />
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
</datalist>
In Chrome/Edge/Safari, clicking on this input once, opens the datalist. However in FireFox, it requires a second click on the input element to open the datalist. Is there a way to get this behavior consistent with other browsers?
The expected result is for a single click to open the data list, just as works in other browsers.

Hide value of a search input option [duplicate]

This question already has answers here:
Setting hidden datalist option values
(4 answers)
Closed 3 years ago.
When using a search input, the list is generally from a database, where the item will have an ID and a title. To make it easier to link what text value is selected from a search input, I usually set the value as the ID, as so:
<input type="search" list="mylist">
<datalist id="mylist">
<option value="1">Example</option>
<option value="2">Test</option>
<option value="3">Another one</option>
</datalist>
But on chrome, and other browsers as far as I know, this results in the value being the "main" thing shown for the options, and the title is a sub-text underneath it.
From an end user perspective this looks awful:
Is there any way to fix this so the "id" or value isn't visible, but that's still the value used when the form is submitted?
JSFiddle
<form action="/action_page.php" method="get">
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit">
</form>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

Disable custom input in HTML input dropdown

Good day!
I've got a simple problem, and it involves HTML.
I've googled everywhere, but found only similar results for, "How to disable autocomplete on input".
I'm trying to find a way to block custom input for a dropdown selection input, but I can't seem to find it.
I've found the attribute disabled, and setting disabled="true" on the input completely disables it, which is not my goal.
This is my code, I found it on w3schools.com.
I've attached 2 versions of it, un-disabled and disabled.
<!--NOT DISABLED STUFF-->
<form action="/action_page.php">
<input style="margin-bottom:15vw;" list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
<!--DISABLED STUFF-->
<form action="/action_page.php">
<input list="browsers" disabled="true">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
Thank you for reading :)
I found a way to do it, thanks to #Eisenheim's comment.
Instead of using <datalist> stuff, it works when I use <select>.
<form action="default.php">
<select>
<option value=""></option>
<option value="Internet Explorer">Internet Explorer</option>
<option value="Firefox">Firefox</option>
<option value="Chrome">Chrome</option>
<option value="Opera">Opera</option>
<option value="Safari">Safari</option>
</select>
<input type="submit">
</form>

Is there a potential way to disable user input in a <datalist>?

I'm debating between using a <select> or <datalist> to display a drop-down list from which the user can select the items.
One downside of the <select> tag is that it's inconsistent as it renders differently in different browsers: some browsers it displays with scroll bar, and for some it's a drop-down list.
The <datalist> on the other hand seems good but I just want to know if there is any way to disable the text input where the user can type whatever they want in the text box if they don't click the down arrow button on the input field as shown:
​<form action="demo_form.asp" method="get">
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit">
</form>
Is there someway to disable the input bar while keeping the dropdown list? I tried the 'readonly' attribute but that renders the whole non-clickable.
You could use the pattern attribute on the input element to restrict the allowed values:
​<form action="demo_form.asp" method="get">
<input list="browsers" name="browser"
pattern="Internet Explorer|Firefox|Chrome|Opera|Safari"
title='Must be "Internet Explorer", "Firefox", "Chrome", "Opera", or "Safari"'>
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit">
</form>

How to add vertical scroll in HTML DataList tag [duplicate]

This question already has answers here:
Scroll bar for Datalist in HTML5
(2 answers)
Closed 7 years ago.
I have a datalist with lot of option, The list goes too long I want to give it vertical scroll.
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
Here the demo Problem
FIDDLE
Unfortunately, there's not much you can do with the datalist attribute. The datalist does not currently support any CSS styling, and specific visual characteristics are browser-specific. Some browsers may choose to add scrollbars for long lists.
If this isn't acceptable, you may have to forget the datalist and implement a dropdownlist and set the list size as per your needs.