fazlurr
3/13/2017 - 1:36 AM

select2 Tagging Support how to dynamically add tag from JavaScript

select2 Tagging Support how to dynamically add tag from JavaScript

// open http://ivaynberg.github.io/select2/
// go down to "Tagging Support"
// #s2id_e12 is a DIV wrapping the whole tagging input

// enter value -> make it think it's pasted on -> fire ENTER key

target = $("#s2id_e12 ul input.select2-input");  // this will fetch input tag
target.val(new Date().getTime());                // type in whatever you want
target.trigger("paste");                         // this even is the key to tag dynamically

event = $.Event('keydown');
event.which = 13;               // this will fire up ENTER key event
target.trigger(event);