1

I am trying to deselect all values of options which allow multiple selection by using this code for chosen plugin

var lstViewCities= $('#lstViewCities');
$(lstViewCities).find("option").each(function(item) {
       $(item).attr('selected', '');
});
$(lstViewCities).trigger("liszt:updated");

but nothing is happening and same for selecting all option

var lstViewCities= $('#lstViewCities');
$(lstViewCities).find("option").each(function(item) {
       $(item).attr('selected', 'selected');
});
$(lstViewCities).trigger("liszt:updated");

[edit]
here is live example for above problem
[/edit]

Doesn't it support selecting multiple options using code or what? Your answers will be surely of great help, looking forward to your responses. Thanks :)

Zaksh
  • 953
  • 2
  • 12
  • 29

3 Answers3

3

As stated here: https://github.com/harvesthq/chosen/issues/1559

You can do this with:

// Deselect
$('#my_select_box option:selected').removeAttr('selected');
$('#my_select_box').trigger('chosen:updated');

// Select
$('#my_select_box option').attr('selected', 'selected');
$('#my_select_box').trigger('chosen:updated');

I just used it and it's working, the problem is if the events get updated in a next release things will just stop working

openjr
  • 31
  • 3
1
$("#btn").click(function(){
    $('.search-choice-close', '.search-choice').trigger('click');
}

In single select you might use:

$("#btn").click(function(){
    $('#testChosen_chzn').find('.search-choice-close').trigger('click');
}
0

try this,

$(lstViewCities).find("option").each(function(this) {
   $(this).attr('selected', 'selected');
});

replace 'this' instead of 'item'

mathi
  • 1,127
  • 12
  • 19