Reputation: 6827
I want to fire an event if I select an option from the select2, but I want it to fire even if the option value is not changed. My select2 version is 3.4.1 Timestamp: Thu Jun 27 18:02:10 PDT 2013
I tried a couple of methods.like.
First i tried,
$('.myselect').on("change", function(e) {
// This function is not triggering if i selected the same option.
});
second, I tried,
$('.myselect').on("select2-selecting", function(e) {
// This function triggers alright but it's giving me my previous selected
// value(i don't want to use timeout function)
});
Upvotes: 1
Views: 2260
Reputation: 19007
As per the documentation the right way to extract the currently selected value is by using the e.val property.
So your code must look like
$('.myselect').on("select2-selecting", function(e) {
alert(e.val); // use this value;
});
Upvotes: 1
Reputation: 706
Please find working solution below for this.
$('.myselect').on("select2-selecting", function(e) {
if(e.val){
//Do your job
}
});
Upvotes: 1
Reputation: 89
you can use the select/unselect event and check with conditions
$('#myselect').on('select2:unselect', function (evt) {...});
Upvotes: 0