80

I'm trying to clear jQuery Chosen dropdown list and refresh it.

HTML:

<select data-placeholder="Select Picture..." class="chosen-select" style="width:250px;" id="picturegallery" tabindex="2">
    <option value="" selected="selected"></option>
    <option value="x">remove me</option>
</select>

When I click "Refresh" button, it should turn into this:

<select data-placeholder="Select Picture..." class="chosen-select" style="width:250px;" id="picturegallery" tabindex="2">
    <option value="1">test</option>
</select>

What I have tried:

$("#refreshgallery").click(function(){
    $('#picturegallery').empty();
    var newOption = $('<option value="1">test</option>');
    $('#picturegallery').append(newOption);
});

But I can't get it to update that dropdown list... Some help? :)

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
plexcell
  • 1,647
  • 2
  • 15
  • 29

7 Answers7

182

Using .trigger("chosen:updated"); you can update the options list after appending.

Updating Chosen Dynamically: If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "chosen:updated" event on the field. Chosen will re-build itself based on the updated content.

Your code:

$("#refreshgallery").click(function(){
        $('#picturegallery').empty(); //remove all child nodes
        var newOption = $('<option value="1">test</option>');
        $('#picturegallery').append(newOption);
        $('#picturegallery').trigger("chosen:updated");
    });
Praveen
  • 55,303
  • 33
  • 133
  • 164
8

If trigger("chosen:updated"); not working, use .trigger("liszt:updated"); of @Nhan Tran it is working fine.

ca michel
  • 105
  • 2
  • 8
  • 1
    [Please use Answers exclusively to answer the question](//meta.stackoverflow.com/q/92107). To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have [sufficient reputation](//stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](//stackoverflow.com/help/privileges/comment). In the meantime, please do not use answers to post comments. – Tim Diekmann Jul 05 '18 at 09:56
  • @TimDiekmann This answer is more useful to me but you're saying he made a mistake. Here we are looking for answers not to Post and Comments unlike Facebook/Instagram/Any other social Medias. `ca Michel` => Thank you. – Silambarasan R Feb 03 '20 at 05:47
4
$("#idofBtn").click(function(){
        $('#idofdropdown').empty(); //remove all child nodes
        var newOption = $('<option value="1">test</option>');
        $('#idofdropdown').append(newOption);
        $('#idofdropdown').trigger("chosen:updated");
    });
dxpkumar
  • 371
  • 2
  • 9
2

If in case trigger("chosen:updated"); doesn't works for you. You can try $('#ddl').trigger('change'); as in my case its work for me.

Monk
  • 21
  • 2
1

MVC 4:

    function Cargar_BS(bs) {
        $.getJSON('@Url.Action("GetBienServicio", "MonitoreoAdministracion")',
                        {
                            id: bs
                        },
                        function (d) {
                            $("#txtIdItem").empty().append('<option value="">-Seleccione-</option>');
                            $.each(d, function (idx, item) {
                                jQuery("<option/>").text(item.C_DescBs).attr("value", item.C_CodBs).appendTo("#txtIdItem");
                            })
                            $('#txtIdItem').trigger("chosen:updated");
                        });
    }
1

In my case, I need to update selected value at each change because when I submit form, it always gets wrong values and I used multiple chosen drop downs. Rather than updating single entries, change selector to update all drop downs. This might help someone

 $(".chosen-select").chosen().change(function () {
    var item = $(this).val();
    $('.chosen-select').trigger('chosen:updated');
});
0

you can use this for reload at the end of page , maybe this one help you

<script>
  $(".chosen-select").chosen();
</script> 
Monu
  • 27
  • 5