If you are not doing the form treatment with javascript
what will be sent in the value of your field <input>
will be the option value (this is default) as demonstrated in the code below (simple demonstration):
let input = document.getElementById('x')
input.addEventListener('input', function(evt) {
if ( input.value !== '' ) {
alert(this.value)
}
}, false)
<datalist id="teachers">
<select>
<option data-value="1" value="Nome 1">Nome 1</option>
<option data-value="2" value="Nome 2">Nome 2</option>
</select>
</datalist>
<input id="x" type="text" list="teachers">
If you take a treatment using javascript
you can take the value of <input>
and seek with querySelector()
an element <option>
that you have in the attribute value=""
an equal value and extract/retrieve content from its attribute data-value
and reallocate this value (from option
) at the <input>
, example:
let input = document.getElementById('x')
input.addEventListener('input', function(evt) {
let selector = document.querySelector('option[value="'+this.value+'"]')
if ( selector ) {
input.setAttribute('value', selector.getAttribute('data-value'))
console.log(input)
}
}, false)
<datalist id="teachers">
<select>
<option data-value="1" value="Nome 1">Nome 1</option>
<option data-value="2" value="Nome 2">Nome 2</option>
</select>
</datalist>
<input id="x" type="text" list="teachers">
PS: the values passed by a form (x-www-form-urlencoded) are {String}
normally compare functions (used on the server) already read the {String}
as if they represent example numbers:
"10" > 5 // verdadeiro
"10" > 15 // falso
However, if you want to save in the database a "numerical" value would be ideal to convert this {String}
numeric to a fact number:
// em PHP
$string_number = "10";
$number = (int) $string_number;
// test
echo(gettype($string_number).' - '.gettype($number));
// output: string - integer
// em JavaScript (Node)
let stringNumber = "10",
number = Number(stringNumber)
// test
console.log(typeof stringNumber +' - '+ typeof number)
// output: string - number
editing:
Ok, the second example modifies the value of the "node" in theDOM
but not the value of <input>
that this in memory ... an approach not very different from this example (the second) uses an event observer submit
for the form.
Observing the event submit
it is possible to replace the value of the <input>
without there being a noticeable change to the user (once the "visible" value will be changed from the name to a "number").
let input = document.getElementById('x')
input.addEventListener('input', function(evt) {
let selector = document.querySelector('option[value="'+this.value+'"]')
if ( selector ) {
this.form.addEventListener('submit', function(evt) {
input.value = selector.getAttribute('data-value')
}, false)
}
}, false)
<datalist id="teachers">
<select>
<option data-value="1" value="Nome 1">Nome 1</option>
<option data-value="2" value="Nome 2">Nome 2</option>
</select>
</datalist>
<form method="post" action="/?">
<input id="x" name="teacher" type="text" list="teachers">
<input type="submit" value="Send">
</form>
The recommendation (if you want to save a number in the database) remains the same.
Tested in:
- Node: version 8.9.4
- PHP: version 7.0
Whether there is a possibility of "conflict" with other elements
<option>
on the page, the ideal would be to pass the query "query"id
of<datalist>
and go through its elements in search of an option that meets the required query (avalue
equal to the value of<input>
)– Lauro Moraes
When I send the value to the form the value sent remains the
value
who is inoption
. And then it goes wrong because the database expects a number and receives a{String}
as, for example, {Name 1}. @Lauromoraes– Wellington Albertoni
@Wellingtonalbertoni have you marked the answer pq managed to resolve? If not, what is your language on the server?
– Lauro Moraes
PHP usage and by the way, the form action is for the same page
action="<?php echo $_SERVER['PHP_SELF']; ?>"
– Wellington Albertoni
When I choose an option from the list, the console appears
<input [...] value="1" type="text">
, he receives invalue
the value that is indata-value
but when sending it does not send THATvalue
, but the list. `– Wellington Albertoni
If you want to take a look, the page code is here, the
input
in question is on the line47
, javascript on the line74
.– Wellington Albertoni
Okay, I made an issue...locally seems to have solved the problem of the second example. Test there and pass a feedback so we can know if solved the question
– Lauro Moraes
Perfect! Solved, thank you very much!!!!!
– Wellington Albertoni