Skip to content

Instantly share code, notes, and snippets.

@github0013
Last active November 9, 2021 13:38
Show Gist options
  • Save github0013/5690297 to your computer and use it in GitHub Desktop.
Save github0013/5690297 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment