Skip to content Skip to sidebar Skip to footer

Jquery Select2 Control - Retrieve Last Selected Element

I am using jQuery select2 control and I need to implement the following functionality: if the user tries to add a certain element, based on some algorithm, I should delete another

Solution 1:

Hey I might be a little late answering this but I found a pretty easy solution to this. You we're right by looking through the event for the last selected item. This worked for me.

var $eventSelect = $('.select_field'); //select your select2 input
$eventSelect.on('select2:unselect', function(e) {
  console.log('unselect');
  console.log(e.params.data.id); //This will give you the id of the unselected attributeconsole.log(e.params.data.text); //This will give you the text of the unselected text
})
$eventSelect.on('select2:select', function(e) {
  console.log('select');
  console.log(e.params.data.id); //This will give you the id of the selected attributeconsole.log(e.params.data.text); //This will give you the text of the selected
})

Solution 2:

for the second part this may help you:

$(this).val(); // references the select

you can filter it to get all needed values.

A Example is in the fiddle: http://jsfiddle.net/jEADR/1588/

Post a Comment for "Jquery Select2 Control - Retrieve Last Selected Element"