Alright folks, this week I wanted to share my quest around the datalist element not properly working on Firefox for Android since... forever?
Let users input food items with a set of suggested options (example from MDN):
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<option value="Chocolate"></option>
<option value="Coconut"></option>
<option value="Mint"></option>
<option value="Strawberry"></option>
<option value="Vanilla"></option>
</datalist>
It works fine with Chrome or Firefox on Desktop but when we start typing on Firefox for Android no suggested option pops up. This is a known bug, opened back in... March 2019: https://bugzilla.mozilla.org/show_bug.cgi?id=1535985 Frustrating!
Spoiler! These unsuccessful tests are assembled in a CodePen.
select element?For some time, it looks like hidding a select element inside the datalist would force the browser to switch to select if datalist was not supported. Something like:
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<!-- select to the rescue -->
<label for="ice-cream-choice-fallback">Choose a flavor:</label>
<select name="ice-cream-choice-fallback" id="ice-cream-choice-fallback">
<!-- Options list for both with textContent -->
<option value="Chocolate">Chocolate</option>
<option value="Coconut">Coconut</option>
<option value="Mint">Mint</option>
<option value="Strawberry">Strawberry</option>
<option value="Vanilla">Vanilla</option>
</select>
</datalist>
This is suggested by DevCurry in 2011 but it's no longer working.
In 2012, Alsacreations also suggests to let some text encourage the reluctant browser to display options:
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<!-- Some text to the rescue -->
Here's what you should see if your browser doesn't support datalist:
<option value="Chocolate"></option>
<option value="Coconut"></option>
<option value="Mint"></option>
<option value="Strawberry"></option>
<option value="Vanilla"></option>
</datalist>
It's no longer working either.
In 2013, someone on stackoverflow pimps it up with some jQuery that I hopefully correctly translated into vanilla JavaScript for this example...
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" />
<datalist id="ice-cream-flavors">
<!-- select to the rescue -->
<label for="ice-cream-choice-fallback">Choose a flavor:</label>
<select name="ice-cream-choice-fallback" id="ice-cream-choice-fallback">
<!-- Options list for both with textContent -->
<option value="Chocolate">Chocolate</option>
<option value="Coconut">Coconut</option>
<option value="Mint">Mint</option>
<option value="Strawberry">Strawberry</option>
<option value="Vanilla">Vanilla</option>
</select>
</datalist>
<script>
const select = document.getElementById("ice-cream-choice-fallback");
const input = document.getElementById("ice-cream-choice");
select.addEventListener("change", e => input.value = e.target.value);
</script>
But I'm afraid it's no better.
After a lot of Duckduckgoing I may have found three leads:
select to show up with a custom dropdown button, maybe with suggestions showing up as typing - see this CodePen for inspiration? Or maybe create a select instead of the input when the detected User Agent is Firefox for Android? Probably too much of additional JavaScript...Also I wonder whether specifying the input type as text may help or if it does not change a thing. Anyway, I guess it's better HTML so it should be there.
In conclusion I'll probably go for the showPicker method. If you have any other idea, I would be glad to hear it!