is it possible to give html5 datalist trigger on given string length?
<input list="tagLookup" id="tag" name="tag" type="text" placeholder="+ add custom tag" autocomplete=off triggerOn="3">
<datalist id="tagLookup">
<option value="aaaa">
<option value="bbbb">
<option value="cccc">
</datalist>
Would like to show suggestion on 3 characters entered by user.
No, there isn't any property that allows you do do this with just HTML. With javascript you could enforce this behavior. The best way seems to be to remove the list=
attribute from the input
as long as you don't need the datalist.
Here's a jQuery example that does what you need:
I added a data-list
attribute with a copy of the list
attribute so that it's easier to restore it after more than 2 characters were typed.
$('input[list]').on('input', function(e) {
var input = $(e.target),
datalist = input.attr('data-list');
if(input.val().length < 3) {
input.attr('list', '');
} else {
input.attr('list', datalist);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input list="myList" data-list="myList">
<datalist id="myList">
<option value="option 1">
<option value="option 2">
</datalist>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With