Roby Sottini
Roby Sottini

Reputation: 2265

jQuery: How to reset Select2 without change event?

I am using Yii2 framework with a select2 component that can let choose its color and show a sample:

Select2 component to select a color

$("#idColor").on("change", function() {
    showSample();
});

Also I have a button to reset the Color select2 component:

$("#idReset").click(function() {
    $("#idColor").val(null).trigger("change");
});

But when I click the Reset button the showSample() function is called. I only need to clear the selected value of Color.

Upvotes: 0

Views: 1500

Answers (1)

Tanmay
Tanmay

Reputation: 1165

Try This:

$("#idReset").click(function() {
    $("#idColor").select2("val",null);
});

This will change the value of select2 dropdown without triggering change event.

Created jsFiddle for the same http://jsfiddle.net/fyhsz9ra/1733/.

Upvotes: 1

Related Questions