Is it possible to give an html5 trigger for a given line length?

Is it possible to give an html5 trigger for a 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>  

I would like to show a sentence for 3 characters entered by the user.

+5
source share
2 answers

No, there is no property that allows you to do this with HTML only. With javascript you can apply this behavior. It seems like the best way to remove an attribute list=from inputif you don't need a datalist.

Here is a jQuery example that does what you need:

I added an attribute data-listwith a copy of the attribute listto make it easier to recover after entering more than two characters.

$('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>
Run codeHide result
+1

- ?

Firefox , (-) - vsync 28 17 21:17

- Firefox...

0

Source: https://habr.com/ru/post/1528968/


All Articles