Support

Account

Home Forums Backend Issues (wp-admin) Add Event to Select2 Field

Solved

Add Event to Select2 Field

  • Hi there, I am trying to hook into the change event of a selec2 field, using the select2_args filter. I have tried many different ways, but the event doesn’t fire. The Documentation for Select2 couldn’t help me a lot with this, either. Can anyone point me in the right direction? This is my code:

    
    (function($) {
    
      acf.add_filter('select2_args', function( args, $select, settings ){
    
        console.log(args); // this works
    
        $select.bind( 'select2-selected', function (e) { 
          console.log("select2:select", e); // doesn't fire
        });
    
        // return
        return args;
            
      });
      
    })(jQuery);
    
  • so… I was able to find a *very hacky* solution:

    
    (function($) {
      
      acf.add_filter('select2_args', function( args, $select, settings, a ){
        
        var selectId = $select.attr('id');
    
        // this is what we need to bind the event to select2:
        var $input = $('input#'+selectId+'-input');
        $input.on( 'select2-selected', function (e) { 
          console.log( 'selected', e );
        });
    
        // return
        return args;
            
      });
      
    })(jQuery);
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Add Event to Select2 Field’ is closed to new replies.