Search code examples
jqueryinputpluginsautocompletetags

How to modify limit number of tags on input with jquery


how to modify the tag plugin https://www.jqueryscript.net/demo/Tag-Input-With-Autocomplete-jQuery-Tagator/

so it only allows selection of x numbers of tags and how to allow only tags that are in the "availableTags-option"?

    $(function () {
            var $input_tagator1 = $('#input_tagator1');
            var $activate_tagator1 = $('#activate_tagator1');
            $activate_tagator1.click(function () {
                if ($input_tagator1.data('tagator') === undefined) {
                    $input_tagator1.tagator({
                        autocomplete: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth'],
                        useDimmer: true
                    });
                    $activate_tagator1.val('destroy tagator');
                } else {
                    $input_tagator1.tagator('destroy');
                    $activate_tagator1.val('activate tagator');
                }
            });
            $activate_tagator1.trigger('click');
        });

enter image description here


Solution

  • $(function () {
      var $input_tagator1 = $('#input_tagator1');
      var $activate_tagator1 = $('#activate_tagator1');
      $activate_tagator1.click(function () {
        if ($input_tagator1.data('tagator') === undefined) {
          $input_tagator1.tagator({
            autocomplete: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth'],
            useDimmer: true
          });
          $activate_tagator1.val('destroy tagator');
        }
      });
      $activate_tagator1.trigger('click');
    });