0
In Javascript, it is possible, through the onchange, oninput and related, detect if a certain input is amended.
Example:
var qs = function (el) { return document.querySelector(el); }
qs("#input").addEventListener('input', function (e) {
console.log('evento "input" %s', e.target.value);
});
qs("#input").addEventListener('change', function (e) {
console.log('evento "change" %s', e.target.value);
});
<input type="text" id="input" />
By tag <datalist> it is also possible to suggest fillers for a input. It is also possible to detect these changes in the input, since selecting an item from a datalist fills the target input.
Example:
var qs = function (el) { return document.querySelector(el); }
qs("#input").addEventListener('input', function (e) {
console.log('evento "input" %s', e.target.value);
});
qs("#input").addEventListener('change', function (e) {
console.log('evento "change" %s', e.target.value);
});
<input type="text" id="input" list="datalist"/>
<datalist id="datalist">
<option>Bola</option>
<option>Peão</option>
<option>Pipa</option>
</datalist>
However, in my scenario, the following need arose: Instead of detecting the changes in input, need to know specifically, through an event, when a option that datalist is selected.
I tried to use onclick and mousedown on the tag option that’s in that datalist, but to no avail.
I need exactly this, not to detect the changes, but to know if the option was selected or not.
Is there any solution ready for this in Javascript?
Observing: The second example I gave does not solve my problem, because the event detects the changes in input, and not if the datalist > option was selected.
I think the event
onselectis what it takes to solve your case.– mutlei
@mutlei This event is to detect text selection, not the choice of an option of a
datalist. What I want is different.– Wallace Maxters
Try the event
changeon the datalist– Costamilam
Good morning friend! When the event takes place
change, does not mean that aoptionwas selected? I could not get well what you want.– Sam
I already understood the context. rs
– Sam
@dvd no, because in fact you could enter a value that is not in the
option.– Wallace Maxters