get value select2 code example

Example 1: get select2 selected value

//initialize
$('#mySelect2').select2('data');
//get selected value
$('#mySelect2').find(':selected');

Example 2: how to set value select2

$('#sel_users').select2().trigger('change');

$(document).ready(function(){

  // Initialize Select2
  $('#sel_users').select2();

  // Set option selected onchange
  $('#user_selected').change(function(){
    var value = $(this).val();

    // Set selected 
    $('#sel_users').val(value);
    $('#sel_users').select2().trigger('change');

  });
});

Tags:

Misc Example