The events differ for Select2 v3 & v4! You could simply call the events of either version to run your code for only that version. If an event doesn't exist, it will be ignored. If you need to test within the change
event, which v3 & v4 have in common, have a look at the code below:
var $el = $('#your-element'),
s2Version3 = false,
s2Version4 = false;
$el.on( 'select2:opening', function() {
// this event only works for v4
s2Version4 = true;
});
$el.on( 'select2-opening', function() {
// this event only works for v3
s2Version3 = true;
});
$el.on( 'change', function() {
if ( s2Version3 ) {
// change things for v3
} else {
// change things for v4
}
});
P.S. To check if a jQuery function is available, isFunction
comes in handy.
var s2Exists = $.isFunction( $.fn.select2 );