I am trying to use “datalist” in html instead of “select”. Most because of the ability to search the list.
The problem is that you can enter a non existing value and I search for a way to validate that the data-value not is empty. Like you type in random text not matching existing data.
My current solution allow non existing values.
Something like this: <input list="jobs" placeholder="Select Job" required=data-value<>"">
This is almost exactly what I was looking for. Thank you!
One problem is however how I should submit the “key” to the database. I guess I then have to iterate once more to get the keys?
I have played with a solution that save the “key” at the “input level”, which is easier to fetch with “getAttribute”. I do not still know if this works when submitting to the database.
Som thoughts on this and further simplifying this?
It should be nice to have the this.setCustomValidity(msg);, but I cannot figure out how to get this to work using for (var element of inputs) .
I am trying to handle a traditional key-value using datalists:
Verify and report that only existing values are allowed.
Store the key in a way that is easy to fetch.
Submit the key to the database.
What I found is that datalists NOT are built for traditional “key-values pair”.
I also found that there are many ways to work around this limitation by looping list, find the key and create hidden field etc.
I have got a solution from @PaulOB that works perfect for #1. But I am still struggling how to fetch values and submit the keys to the database. In order to make it easier to fetch I assume that the keys are easier to fetch when stored in at the input level.
Storing the keys in input makes it simpler to verify as I only have to loop through the input elements (not all options)