jQuery Chosen plugin add options dynamically

SBel picture SBel · Jul 5, 2012 · Viewed 81.2k times · Source

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);

Answer

Bojangles picture Bojangles · Jul 5, 2012

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.