Menu
I am trying to use “datalist” in html as a “select”.
The value selected in the datalist should be verified by a “data-value”.
So, there should not be any ad-hoc entering. You must select a existing value.
My current solution allow non existing values.
Something like this:
`<input list=”jobs” placeholder=”Select Job” required=”data-value”>
Fiddle:
Is this possible?
> <!DOCTYPE html><html lang="en"><head><title> Test Page </title>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>
<!--
From:https://www.sitepoint.com/community/t/validate-data-value-in-a-html-datalist/355469
And:https://jsfiddle.net/brsf14yu/
-->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300 ">
<style>
/* datalist { display: block; } /* for testing purposes only -- normally display: none */
${respJob.value}n${respCode.value}n${respNote.value}n${respCar.value}
``<i>
</i><label for="browser">Choose your browser from the list:</label>
<input list="browsers" name="browser" id="browser">
<datalist id="browsers">
<option value="Edge">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<script>
browser.onchange=function(){
for(var i=0;browsers.options.length;++i){
if (typeof browsers.options[i]=='undefined'){break ;}
if (browser.value == browsers.options[i].value) break;
}
if(i == 5){browser.value = ''}
}</script><i>
</i>
``
>@JMRKER#1621273 Was that answer not acceptable? Are you trying to do some sort of coder vs. coder comparisons?
> I played around with the code and came up with the following.
> The approach is a bit different from your original post but can be modified as needed if you see the purpose of the suggested changes.
>@JMRKER#1621301 I don't know how to use this forum thread to send the correct file contents without re-creating the same errors.
I don't know how to use this forum thread to send the correct file contents without re-creating the same errors.[/quote]Another way is using code tags when posting code:
your code here
`<i>
</i><!DOCTYPE html><html lang="en"><head><title> Test Page </title>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>
<!--
From: https://www.sitepoint.com/community/t/validate-data-value-in-a-html-datalist/355469
And: https://jsfiddle.net/brsf14yu/
-->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300">
<style>
/* datalist { display: block; } /* for testing purposes only -- normally display: none */
* {
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
font-size: 18px;
}
input[type=text],
input[list]{
color: #f0f0f0;
font-size: 18px;
padding: 10px;
max-height: 56px;
width: 350px;
background: #555;
-webkit-appearance: none;
appearance: none;
border: none;
}
#submit{
color: #f0f0f0;
font-size: 18px;
padding: 10px;
max-height: 56px;
width: 370px;
background: #555;
-webkit-appearance: none;
appearance: none;
border: none;
}
</style>
</head><body>
<form action="javascript:alert('Success')" method="post"
onsubmit="return checkInputs()"> <!-- target="_self" -->
<br/>
<input id="job" list="jobs" placeholder="Select Job" required="data-value">
<datalist id="jobs"></datalist>
<br>
<input id="code" list="codes" placeholder="Select Code" required="data-value">
<datalist id="codes"></datalist>
<br> <br/>
<input id="note" type="text" placeholder="Note" required>
<br>
<input type="submit" value="Submit"> <!-- onsubmit="return checkInputs()" -->
<br>
Remove following after testing<br>
<!-- for testing purposes -->
<input id="auto" list="autos" placeholder="Car" required="data-value" />
<datalist id="autos"></datalist>
</form>
<script>
console.clear();
function checkInputs() {
var respJob = document.getElementById('job'),
respCode = document.getElementById('code'),
respNote = document.getElementById('note');
var respCar = document.getElementById('auto'); // for testing purposes only
const errMsg = 'Please select a valid value';
var err = 0; // represents no errors
if (!listJobs.includes(respJob.value)) { err = 1; respJob.setCustomValidity(errMsg); }
else { respJob.setCustomValidity(''); }
if (!listCodes.includes(respCode.value)) { err = 2; respCode.setCustomValidity(errMsg); }
else { respCode.setCustomValidity(''); }
if (!listCars.includes(respCar.value)) { err = 3; respCar.setCustomValidity(errMsg); }
else { respCar.setCustomValidity(''); }
if (err != 0) { // error noted
alert('Error - invalid entry '+err+' - '+errMsg);
return false;
} else { // all OK
alert(<span><code>${respJob.value}n${respCode.value}n${respNote.value}n${respCar.value}</code></span>);
return true;
}
// return (err !== 0);
}
function createOptions(listIDS,Ary) {
const list = document.getElementById(listIDS);
Ary.forEach(item => {
let option = document.createElement('option');
// [n,v] = item.split(':'); option.value = item.replace(':',' '); option.textContent = v;
option.value = item; option.textContent = item;
list.appendChild(option);
});
}
const listJobs = ['42 Web','43 Print'];
const listCodes = ['Project Management','COPY'];
const listCars = ['1:Chevrolet','2:Dodge','3:Ford','4:General Motors','5:Nissan','6:Toyota'];
function init() {
createOptions('jobs',listJobs);
createOptions('codes',listCodes);
createOptions('autos',listCars);
} init();
</script>
</body></html>
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/>
``<i>
</i><form onsubmit='return val(false)'>
<label for=browser>Choose your browser from the list:</label>
<input list=browsers name=browser id=browser data-noadhoc>
<datalist id=browsers>
<option data-id=1 value="Edge">
<option data-id=2 value="Firefox">
<option data-id=3 value="Chrome">
<option data-id=4 value="Opera">
<option data-id=5 value="Safari">
</datalist>
<button>Send</button>
</form>
<script>
window.oninput=function(e){
if(e.target.dataset.noadhoc == undefined)return;
var list=e.target.list;
(function chek(ok){
for(var i=0;i<list.options.length;++i)
if (e.target.value == list.options[i].value.substring(0,e.target.value.length))ok=true;
if(!ok){e.target.value = e.target.value.substring(0,e.target.value.length-1);chek();}}
)(false);
}
function val(ok){
for(var i=0;i<browsers.options.length;i++)
if(browsers.options[i].value == browser.value){
ok=true;browser.value=browsers.options[i].dataset.id;}
if(!ok) alert('Failed');
return ok;
}
</script><i>
</i>
``
0.1.9 — BETA 4.18