Does somebody have an easy to use example of a country drop-down, with country flags, for Select2
? I am about to implement one based on this suggestion, but I would prefer to avoid reinventing the wheel.
Select2 drop-down for countries, with flags
Asked Answered
I was working on a similar problem and here is how I solve it.
(function($) {
$(function() {
var isoCountries = [
{ id: 'AF', text: 'Afghanistan'},
...
];
//Assuming you have a select element with name country
// e.g. <select name="name"></select>
$("[name='country']").select2({
placeholder: "Select a country",
data: isoCountries
});
});
})(jQuery);
I also have made a gist about it and following are the demos.
- Demo 1
- Demo 2 with flags based on official DOC of
select2
The Select2; Examples - Templating shows the flag when you choose a country as the selected option in the non-drop-down view. I've copied what they've done, similar to your Demo 2 with flags however this does not show the flag upon chosing an option. How can I replicate this please? –
Chu
Awesome solution - works for my Chrome page action extension –
Tolentino
@TimMarshall If you still haven't fixed it, can I see some code with what you have attempted? –
Counterirritant
@GregTrevellick, Great. I am glad. Let me know once you finish your extension. I would like to see it. –
Counterirritant
@Starx, thank you for this solution, it helped me a lot. I know this answer is a bit old but I wanted to answer @Tim Marshall because I faced the same problem and the solution could help others. Firstly, you have to make sure to add the flag-icon CSS library (
<link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.3.0/css/flag-icon.min.css" rel="stylesheet" />
in 2019). –
Gloriane Secondly, if you want to see the flags on both the dropdown options and the selected option, you just have to add 2 lines:
templateSelection
for dropdown and templateResult
for selected option. So you will end up with this: $("[name='country']").select2({ placeholder: "Select a country", templateSelection: formatCountry, templateResult: formatCountry, data: isoCountries });
–
Gloriane The way i did it:
<div class="form-group">
<label class="control-label">Destination</label>
<input type="text" name="cdCountry" class="form-control" required />
</div>
<script>
$("[name='cdCountry']").select2({
placeholder: "Select a country",
formatResult: function (country) {
return $(
"<span><i class=\"flag flag-" + country.id.toLowerCase() + "\"></i> " + country.text + "</span>"
);;
},
data: yourDataSource
});
</script>
and using the css library (css and a sprite) https://www.flag-sprites.com/
Have a look on the following links.
http://www.marghoobsuleman.com/countries-dropdown-flags http://vincentlamanna.com/BootstrapFormHelpers/country.html
second link is dead =[ –
Humid
first link is a good library but it has a lot of bugs and it is not responsive –
Commensal
@VivienPipo yes it is not responsive but it is very helpful and for responsiveness you need to make them responsive like change the width of .dd class –
Noreennorene
<head>
<link href="select2.css" rel="stylesheet"/>
<script src="select2.js"></script>
<script>
$(document).ready(function() { $("#e1").select2(); });
</script>
</head>
<body>
<select id="e1">
<option value="1">Albania<img src="Albania.jpg"></option>
...
<option value="2">Germany<img src="Germany.jpg"></option>
</select>
</body>
Thanks, but this is too raw. I still need to put together the images (jpgs, no way!), the list of countries, etc. I was looking for some javascript library where I can simply pass an array of countries (by ISO code, for example), and get a fully initialized
Select2
drop-down, with flags. –
Iveyivie © 2022 - 2024 — McMap. All rights reserved.