61

I make a jQuery Chosen drop-down like this:

$('.blah').chosen();

I can't find how I can add options, something like:

$('.blah').chosen('add', name, value);
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
SBel
  • 3,315
  • 6
  • 29
  • 47

5 Answers5

144

First, you need to add the <option>s to the <select> that Chosen was bound to. For example:

$('.blah').append('<option value="foo">Bar</option>');

Then, you need to trigger the chosen:updated event:

$('.blah').trigger("chosen:updated");

More information can be found here (although you need to scroll down to Change / Update Events).


Update 7th August 2013

The event name has changed to chosen:updated since version 1.0 (July 2013) as Tony mentions in the comments. The updated documentation can be found here.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • 3
    @Mike Sorry about that `;)` it happens to me all the time! – Bojangles Jul 05 '12 at 20:42
  • 11
    Starting with version 1.0 which the trigger is now "chosen:updated". See http://harvesthq.github.io/chosen/#change-update-events – Tony Aug 07 '13 at 21:32
  • I suggest you update your answer by replacing $('.blah').trigger("liszt:updated"); with $('.blah').trigger("chosen:updated"); - because people like me read the code only – Dustin Sun Nov 01 '17 at 15:07
  • 1
    @lonelyloner Thanks, I've updated the code examples in my answer – Bojangles Nov 02 '17 at 11:24
46

newest chosen version changed the event name to "chosen:updated"

so your code will be like this:

$('.blah').append("<option value='"+key+"'>"+value+"</option>");
$('.blah').val(key); // if you want it to be automatically selected
$('.blah').trigger("chosen:updated");
aluksidadi
  • 483
  • 5
  • 9
4

You can call this function to add element to chosen after you save the element to server using Ajax:

function appendToChosen(id,value){
    $('.blah')
        .append($('<option></option>')
        .val(id)
        .attr('selected', 'selected')
        .html(value)).trigger('liszt:updated');
}

Ajax call:

$.ajax({
    type: 'POST',
    url: 'savepage.php',
    data: $('#modal-form form').serialize(),

    success: function(data, status) {
        appendToChosen(data[0],data[1]);
    },
    error: function (response) {
        alert(response);
    }
    }).always(function(data, status) {
        //hide loading
    });
TotPeRo
  • 6,561
  • 4
  • 47
  • 60
1

Try this..

         $.ajax({
            url: "@Url.Action("Actionname", "Controller")",
            data: { id: id },
                dataType: "json",
                type: "POST",
                success: function (data) {
                $("#id_chzn .chzn-results").children().remove();
                var opts = $('#id')[0].options;
                    $.map(data, function (item) {
                        var text = item.text;
                        for (var i = 0; i < opts.length ; i++) {
                            var option = opts[i];
                            var comparetext = option.innerText;
                            var val = 0;
                            if(text == comparetext)
                            {
                                val = option.index;
                                $('#id_chzn .chzn-results').append("<li id='id_chzn" + val + "' class='active-result' style>" + item.text + "</li>");
                            }
                        }
                    });
                  }
                });
muthu
  • 59
  • 1
  • 1
  • 5
0

I used below code to update choosen dropdown options dynamically and it works:

var myDropDownOptionHtml ='<option value="0">--Customer--</option>'; 

$('#myDropdownId').html(myDropDownOptionHtml );
setTimeout(function() {
    $("#myDropdownId").trigger("liszt:updated");
},100);

FYI, I am using jQuery Chosen Version 0.13.0

Happy Coding!

Deepak Goswami
  • 2,030
  • 1
  • 16
  • 22