Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to give html5 datalist trigger on given string length?

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.

like image 329
Monnster Avatar asked Nov 11 '22 12:11

Monnster


1 Answers

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>
like image 143
Stephan Muller Avatar answered Nov 15 '22 04:11

Stephan Muller