Get selected option in Select2 event, when multiple options can be selected
Asked Answered
G

2

5

How can I get hold on the <option> that was just selected when listening to the select2:select event? Note that this is simple when using a single-select, as when only one option is selected, that must be the one that was just selected. I would like to also be able to find the option that was just selected when using a multiple-select (<select multiple>).

In the select2:unselect event, the unselected <option> is available through e.params.data.element, but it is not so in the select2:select event. I do not see a reason why the <option> should not be available, since it is created at this time. For the select2:selecting event, however, the <option> is not yet created, and obviously cannot be available when the event is fired.

Glebe answered 21/5, 2016 at 16:5 Comment(0)
H
25

I've used the following to get the current selected in Select2 (it's for version 4 and up):

// single value
var test = $('#test');
test.on("select2:select", function(event) {
  var value = $(event.currentTarget).find("option:selected").val();
  console.log(value);
});

UPDATE: Multi Selected Values (with and without last selected)

// multi values, with last selected
var old_values = [];
var test = $("#test");
test.on("select2:select", function(event) {
  var values = [];
  // copy all option values from selected
  $(event.currentTarget).find("option:selected").each(function(i, selected){ 
    values[i] = $(selected).text();
  });
  // doing a diff of old_values gives the new values selected
  var last = $(values).not(old_values).get();
  // update old_values for future use
  old_values = values;
  // output values (all current values selected)
  console.log("selected values: ", values);
  // output last added value
  console.log("last added: ", last);
});
Hopson answered 30/9, 2016 at 8:40 Comment(9)
Won't this just find the value of the first option among the options that are selected? I would like to get the option that was just selected, also if there are multiple options selected.Glebe
Than I suggest not using the .val();Hopson
Yes. To get the option that was just selected, I would have to keep a list of all selected options, and see which option is now selected, but is not in the list. Or do you have a better idea?Glebe
@MagnarMyrtveit I had to revisit this, and have added the multi values retrieval as well. Good Luck!Hopson
Thanks for the update, but the question is still not answered. I want the option that was most recently selected, not all selected options.Glebe
@MagnarMyrtveit that's easy, just add a variable outside of the event handler scope, and diff the resulting arrays. I've updated the example above.Hopson
How to do this, without the last selected, but all?Oribelle
@Oribelle do you mean all values? It's in the example above. Maybe you should put some console.log statements than you'll see where the values come out.Hopson
Works fine, except for event defintion. I had to change event from select2:select to change. And in most cases its more useful to get val(). The rest is fine!Dhobi
T
-1
 $('#test').on('select2:select', function(e) {
  var data = e.params.data;
  console.log(data);
 
});
Thermophone answered 6/1, 2021 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.