select2 trigger to select items using jquery

62 Views Asked by At

I have 3 select2 element in one page. My aim is to update all these 3 select2 when I select an item in any of them.

I made it work for unselect event as this code, but having trouble when updating on select event:

var $multipleSelect = $(".multiple-product-list").select2({
  ... more codes here
});

$(document).on("select2:unselect", ".multiple-product-list", function(e){
  // Remove selected item in all select2
  $multipleSelect.val([array_of_ids_here]).trigger("change");
});

$(document).on("select2:select", ".multiple-product-list", function(e){
 // Needs to update all select2 w/o duplication
 // Add selected item to all select2.
});

For example, i selected Product 20 it shoud update my 2 other select2 on the page.

enter image description here

Here's a sample generated element for my select2.

enter image description here

0

There are 0 best solutions below