Pawan
Pawan

Reputation: 529

Dynamically adding data to select using jQuery chosen plugin

I am facing a problem of dynamically adding options to multiple select using chosen plugin.

As I have millions of records in database so I can not populate all as options during page load.

so now I have to get matching records (on keyup event) from database using ajax and populate select box dynamically. is there any way i can achieve this.

Upvotes: 0

Views: 4767

Answers (2)

DotNetLover
DotNetLover

Reputation: 229

We can easily append option to chosen by using the following option.

<select class=”chosen-select”>
<option value=”101”>Hyderabad</option>
<option value=”102”>Banglore</option>
<option value=”103”>Chennai</option>
</select>

$(".chosen-select").append('<option value="104”>Delhi</option>');
$(".chosen-select").trigger("chosen:updated");

For more information related to chosen jquery dropdown you can click here http://www.dotnetqueries.com/Article/151/how-to-implement-chosen-plugin-with-example

Upvotes: 0

Gal Bracha
Gal Bracha

Reputation: 20011

You can just select the option you want manually by value as such:

$("#form_field").val('value_to_be_selected');

and then call

$("#form_field").trigger("liszt:updated");

Upvotes: 2

Related Questions