I am trying to make a simple <select>
, containing two images, but it turns out that it is not so simple since putting a label <img>
inside a <option>
is not possible, nor does it tolerate <span>
. I have looked in the forum in English and I have found this answer which has led me to make this fiddle to check its functionality.
<select class="form-control input-lg" id="exampleFormControlSelect1">
<option style="background-image:url(https://cdn.countryflags.com/thumbs/portugal/flag-800.png);">
Português
</option>
<option style="background-image:url(https://cdn.countryflags.com/thumbs/spain/flag-800.png);">
Español
</option>
</select>
This solution is not valid because it was only for firefox but it is no longer valid, it suggests that we use external libraries like JQueryUI but I can't use libraries
Why is this happening and how can I make it work?
It is not possible to apply styles to the label
<option ... />
and it is necessary to look for other alternatives, usually an unordered list<ul ... />
In the html, just change the attribute
style
of each option to use it as a datasetdata-style
and be able to take it from javascript. All the code has comments so you know what is done in each step, it doesn't work completely like a<select>
, but it can be a good option.Trying to make it work as close as possible to a
<select>
, adding another element that contains the current value and needs to be clicked to display the list:The fastest way would certainly be using JQuery, however it is not mandatory, a suggestion is that you do not put the image in the select.
In this code you can see how it would be done with a div where you put the image.