dVeza
dVeza

Reputation: 552

Select2 get option value that triggers selecting/unselecting

https://select2.github.io/options.html#what-events-will-select2-trigger

The documentation, says that select2:selecting and select2:unselecting can be prevented. I want to prevent the event conditionally depending on the value being selected/unselected.

$('select').on('select2:selecting', function(e) {
  // what value is currently being selected?
  // $(this).val() does not yet have it and I should set it conditionally
})

Upvotes: 1

Views: 1230

Answers (1)

teo van kot
teo van kot

Reputation: 12491

All info about selected option contains e variable of your function so you can get it like this:

$('select').select2().on('select2:selecting', function(e) {
  console.log(e.params.args.data.id); //value of selected  option
  console.log(e.params.args.data.text); // text of selected option
});

Check this jsFiddle.

Upvotes: 1

Related Questions