CrApHeR
CrApHeR

Reputation: 2655

Cannot retrieve the value to be selected on select2:selecting

I am using Select2 4.0.3 with single selection.

I am trying to get the value to be selected (the one that the user select and will be assigned) before the change event is triggered.

This is code I am using to hook the event and what I tested:

$('#mycombo').on("select2:selecting", function (evt) {

  var $this = $(this);

  console.log($this.val()); // This return the current value
  console.log(evt.data);    // This return unassigned
  console.log(evt.params);  // This return unassigned
  console.log(evt.args);    // This return unassigned
  console.log(evt.object);  // This return unassigned      
})

I also tried the example here Fire callback when selection was made with select2 4.0, and retrieve the value of last selection but it look like the select and change events are triggered simultaneously.

I am lost and any help will be really appreciated.

Upvotes: 1

Views: 63

Answers (1)

xploshioOn
xploshioOn

Reputation: 4115

You can have that information that is in params, try for example with this

$('#mycombo').on("select2:selecting", function (evt) {
  console.log(evt.params.args.data);  // this returns the object of 
})

and then in data you can do something like .text or .id and you have the text of the option or the value respectively.

evt.params.args.data.text

Upvotes: 2

Related Questions