Categories
JavaScript Answers

How to set selected value of jQuery Select2 with JavaScript?

Spread the love

To set selected value of jQuery Select2 with JavaScript, we call val and then trigger.

For instance, we write

$("#drpServices").select2().val("0").trigger("change");

to create the drop down with

$("#drpServices").select2()

Then we set its value with the val method to the option with value attribute set to 0.

Finally, we call trigger with 'change' to update the drop down to show the value we set.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *