jquery - Auto tokenize last item in pasted string using select2 -


i using jquery select2 create input allows auto tokenization. i've initialized select2 input following options:

{     tags: [''],     tokenseparators = [',', ' '] } 

very basic stuff. when type string of text input, followed comma or space, preceding string tokenized, expect.

however, need support pasting of text input. things fall apart.

if paste '1,2,3,4' input, separate tokens 1, 2, , 3, 4 not tokenized. instead, remains value of input.select2-input , when focus changes value disappears.

i have tried number of different approaches, no avail.

i have attempted intercepting paste event, getting pasted string's value e.originalevent.clipboarddata.getdata('text/plain'), cancelling event, , adding comma end of string before changing input's value timed out .select2('val', str).

i have tried simulating comma keypress event after paste event. (.trigger({type: 'keypress', which: 188}))

i have tried changing value of input.select2-input each item in array created splitting paste string.

nothing seems work. ideas?

based on question have posted, can use following code works variations:

1 2 3 4

1,2,3,4

1,2 3, 4

you can check jsfiddle. please note: i've found version 3.5.0 has bug third variation, should use latest 3.5.1.

$("#select2-input").select2({     tags: [''],     tokenizer: function(input, selection, callback) {         if (input.indexof(',') < 0 && input.indexof(' ') < 0)             return;          var parts = input.split(/,| /);         (var = 0; < parts.length; i++) {             var part = parts[i];             part = part.trim();              callback({id:part,text:part});         }     } }); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -