javascriptjqueryhtmljquery-tokeninput

Set maxElements property in select2 tokenizer using HTML


I use jQuery Tokenize for selecting multiple values in drop down.My requirement is that i want to disable drop down after user select one element.

HTML Code

<select id="myinput" multiple="multiple"class="tokenize-sample" required="required" maxElements=1></select>

To set properties to drop down we can use following code snippet.But in my case it will draw a another drop down with existing one.So i want to add properties to HTML code like above code snippet.

<script type="text/javascript">
$('#myinput').tokenize({
    maxElements: 1
});
</script> 

This is how i call methods on drop down and this is working fine.but my requirement is add properties on drop down after initialization.

$("#myinput").data('tokenize').disable();

if anyone knows how to achieve this please let me know.


Solution

  • Maybe I misunderstood but if you want to disable the Tokenize after adding an element you can do this :

    $('#myinput').tokenize({
      onAddToken: function() {$("#myinput").data('tokenize').disable();},
      maxElements: 1
    });
    

    https://jsfiddle.net/e6s96v2s/