select2-selecting event not getting fired
Asked Answered
A

3

5

I am using Select2 in my website, and I'm trying to use the select2-selecting event, but its not firing. I am also using Backbone.js in the app, so the first thing I tried was adding the select2-selecting to my events object:

// 'change .city':'cityChanged'
'select2-selecting .city':'cityChanged'

Note that I have a change event commented out - this change event works properly. In the documentation for Select2, the select2-selecting event is put directly on the object, not like this:

$('.city').select2().on('select2-selecting', function(e){ 
     console.log('here');
});

instead, its supposed to be used like this:

$('.city').on('select2-selecting', function(e){ 
     console.log('here');
});

I have also tried adding the event both of these ways, but the event didn't fire (I did check and the element was created on the DOM before I added the events).

When I add the event in the first method with the Backbone.js, the event is listed in the event listeners in the chrome debug console - it just doesn't get fired. Does anyone have an idea what is going on?

Antipole answered 9/7, 2013 at 14:25 Comment(0)
C
17

what version of select2 are you using?

I was having the same problem until I realize I was using the 3.3 version where this select2-selecting event not exists.

This has been included in the 3.4 version.

Carlettacarley answered 10/7, 2013 at 14:26 Comment(1)
i'm using select2 3.4.3 and i have the same problem none of the events are triggered !!Partial
R
12

There was a change on earlier versions also where it changes name:

  • select2-close is now select2:close
  • select2-open is now select2:open
  • select2-opening is now select2:opening
  • select2-selecting is now select2:selecting
  • select2-removed is now select2:removed
  • select2-removing is now select2:unselecting
Ressler answered 5/10, 2015 at 16:40 Comment(1)
This is correct for 3.5, no idea why the documentation does not include this.....Verine
D
0

On even older versions, 'select2-removed' and 'select2-removing' events listed on @santi-iglesias' answer doesn't exists. You have 'removed' instead. Also, to get the affected optioin value, use 'event.val'.

So you can do something like this:

$('.select').on('select2-selecting removed', function(evt) {
    var value = evt.val; //do something with this
});

Checked at v3.4.3.

Discommodity answered 16/3, 2017 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.