Reputation: 4100
I have manage to get id of selected option for the Chosen plugin. Here is the jsfiddle Demo.
Now I am not sure how to get the Id of unselected option. I am using this code to get the id of selected option.
var SelectedIds = $(this).find('option:selected').map(function() {
if ($(this).attr('value') == params.selected)
return $(this).prop('id')
}).get();
alert(SelectedIds);
Upvotes: 3
Views: 1089
Reputation: 1210
When an option is deselected, you get the change event, but the params
object has a deselected
property that you can use just like you're using the selected
.
I made a jsfiddle for you to demonstrate: http://jsfiddle.net/1eut1c3d/
$("#chosen").chosen().on('change', function(evt, params) {
if (params.selected !== undefined) {
var selectedID = $(this).find('option:selected').map(function() {
if ($(this).attr('value') == params.selected)
return $(this).prop('id')
}).get();
alert("Selected: " + selectedID);
}
if (params.deselected !== undefined) {
var deselectedID = $(this).find('option').not(':selected').map(function() {
if ($(this).attr('value') == params.deselected)
return $(this).prop('id')
}).get();
alert("Deselected: " + deselectedID);
}
});
Upvotes: 6