Preselect Option from Dynamically Created Option List

Hi,

I have a dynamically created ( very large ) option list that once done loading and adding select2() want to preselect one of the values from the list.

Have tried a bunch of methods but can seem to get it working. Any ideas?

Thanks!

Are you dynamically generating HTML <option> elements and inserting them into the DOM, or are you feeding a JavaScript array of data items to the select2() initialization call? If you’re creating <option> element, then you should be able to include the selected attribute on the <option> that you want to be selected. If you’re using a JavaScript array, then you just need to include "selected": true in the data item that you want to be selected.

If you’ve tried those things, then please post the code you’re using so I can take a look at it.