Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger tag creation in select2 single select (e.g. when adjacent button is pressed)?

I am using select2 4.0.3. I have a select2 box to enter email addresses, and I would like to make sure that all email addresses are included in .val(), also the one that the user was still typing in the search field.

Form element

The form field is referenced by $('.invite-emails-field'). When I press the Send button, in the event handler $('.invite-emails-field').val() just gives me the first two addresses [email protected] and [email protected], but not the third address ([email protected]).

This is how I initialize the select2 element:

$('.invite-emails-field').select2({
  tags: true,
  tokenSeparators: [',', ' '],
  selectOnBlur: true
});

The selectOnBlur has no effect, and I cannot find anything else that works on select2 v4. I tried firing several events at various elements, none of it worked.

I expect that when I press the Send button, that I can make some call to the select2 box to trigger creating a tag for the contents of whatever is then in the search field [email protected], and that subsequently .val() returns an array with all three addresses.

Update: I created a jsFiddle for you to play with. Enter input like this:

View of jsFiddle before pressing Send button

and then press the Send button, you will see:

View of jsFiddle after pressing Send

where [email protected] is missing from the output.

Note that in my real application I have disabled the dropdown because I just want the tagging behaviour.

like image 580
Martijn de Milliano Avatar asked Oct 12 '16 10:10

Martijn de Milliano


People also ask

How do I create a select2 dynamically?

HTML. Create a <select class="select2_el" > element to initialize select2 on page load and create <div id="elements" > container to store <select > element on button click using jQuery AJAX.

What is select2 tag?

Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

How do I add options in select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).

What select2 dropdown?

By default, Select2 will attach the dropdown to the end of the body and will absolutely position it to appear above or below the selection container. Select2 will display the dropdown above the container if there is not enough space below the container, but there is enough space above it.


1 Answers

You need to add selectOnClose and set it to true so that it creates the tag for you when you close / click off of the search input.

$('select').select2({
  selectOnClose: true
});

See the official documentation here: https://select2.github.io/options.html#can-i-select-the-highlighted-result-when-the-dropdown-is-closed

like image 71
Luke Avatar answered Sep 21 '22 05:09

Luke