David Miller
David Miller

Reputation: 27

How do I clear all items in a Chosen.js multiselect dropdown?

I want to REMOVE all items in the chosen.js multi select drop-down, How can I do this? I tried using..

var ddlSitesID = $("#SelectedSites option").length
if (ddlSitesID) {
    for (i = 0; i != ddlSitesID; i++) {
        $("#SelectedSites option").remove(i);
    }
}

But it does not work, please help.

Upvotes: 0

Views: 2256

Answers (4)

procma
procma

Reputation: 1450

$("#SelectedSites").val('').trigger("chosen:updated");

Upvotes: 0

David Miller
David Miller

Reputation: 27

I found the solution,

First run this..

    var ddlSitesID = $("#SelectedSites option").length
    if (ddlSitesID) {
        for (i = 0; i != ddlSitesID; i++) {
            $("#SelectedSites option").remove(i);
        }
    }

then run this after it

$('#SelectedSites').trigger('chosen:updated');

Upvotes: 0

Bharatsing Parmar
Bharatsing Parmar

Reputation: 2455

Try this: jsfiddle.net/bharatsing/j9yuL/131/

//To clear selection
$('#SelectedSites option').prop('selected', false).trigger('chosen:updated');

//Remove all items
$('#SelectedSites').html("");    
$("#SelectedSites").chosen().trigger('chosen:updated');

Upvotes: 2

Mubariz Feyziyev
Mubariz Feyziyev

Reputation: 412

You can do this with two way

-First way:

      $('#SelectedSites').html('');

-Second way:

      $('#SelectedSites').find('option').remove().end();

Upvotes: 1

Related Questions