Selecting a Select2 Option Using Javascript

Copying the same template as
Solution 2:
This code is for getting selected country value in dropdown with country flag in it using ip address of the server and by using google plugins ..
I know it’s been a while, but there is a workaround for this.
Turns out that if you add an empty
to the select tag, and add a placeholder, the issue is fixed.
You can use
this will give you option tag html then using this you can get the
value and compare it with all options attr if not same disabled that option.


By examining the origin of the fiddle, it becomes evident that the select2 library resource versions vary.

Check out this link for Version 3.5.2:

Check out the 4.0.5 version at

An update has been made to ensure compatibility with version 4.0.5.

You can access the functional fiddle via this link:

To bind data, you should include a data attribute to


. Additionally, make sure to add an empty


tag in the HTML to avoid selecting the first element by default.

   minimumInputLength: 0,

Frequently Asked Questions

Posted in Uncategorized