2

Question

I want to make my select tag like Google's. This is mine:

i try to put the flag on background-image because i research at forum everyone say like that

This is my code

<script >
        function format(state) {
            if (!state.id) return state.text;
            return "<img class='flag' src='Photos/" + state.id.toLowerCase() + ".jpg'/>" + $(originalOption).data('foo') + "' />" + state.text;
        }
</script>
<select id="mobilephone">
    <option value="indo" ><img src="Photos/indo.jpg" id="captchaimg">Indonesia</option>
    <option style="background-image:url(Photos/rico.png);">Rico</option>
    <option style="background-image:url(Photos/SA.png);">South Africa</option>
    <option style="background-image:url(Photos/UK.jpg);">United Kingdom</option>
</select>

I want to make it like this:

enter image description here

isherwood
  • 58,414
  • 16
  • 114
  • 157
Kira Katou
  • 158
  • 2
  • 2
  • 16
  • 1
    Take a look at [JavaScript image combobox](http://www.marghoobsuleman.com/jquery-image-dropdown) CSS won't work because different browsers treat `select` dropdowns differently. – APAD1 May 24 '16 at 18:06
  • @APAD1 Doesnt work dude, already follow the step – Kira Katou May 24 '16 at 18:22
  • 1
    It works fine, I have used that library many times. Here's an exmaple page that has exactly what you're trying to do: http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/samples/index.html – APAD1 May 24 '16 at 18:30
  • @APAD1 cannot understand dude. can't you give me more easier example ? from your maybe ? just important part plz :D – Kira Katou May 24 '16 at 18:59
  • @KiraKatou read the source code on the URL linked by APAD1 – Martin May 24 '16 at 19:00
  • what happens if you add `no-repeat left` at the end of your `background-image: url(...)` ? – blurfus May 24 '16 at 19:04
  • @ochi the images is gone – Kira Katou May 24 '16 at 19:12
  • @ochi i try to use background only and its work but in the box didnt show the flag :( – Kira Katou May 24 '16 at 19:17

2 Answers2

0

If you are using jQueryUI, then this may work : http://jqueryui.com/selectmenu/#custom_render

This seems quite neat : http://www.jankoatwarpspeed.com/post/2009/07/28/reinventing-drop-down-with-css-jquery.aspx

You cannot do this directly using the <select> tag since that is a form element, but you can implement this using <div> or other methods.

A work-around to this could be :

<select>
    <option style="background-image:url(something.png);">male</option>
    <option style="background-image:url(something.png);">female</option>
    <option style="background-image:url(something.png);">others</option>
</select>

But this would only work in Firefox :(

Personally I would recommend using some JS widget library like the one above.

Hope this helps!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chirag Galaiya
  • 87
  • 1
  • 1
  • 10
0

You can simply use the emoji for each flag.

<select id="mobilephone"> 
    <option selected disabled>Select country</option>
    <option> Indonesia</option>
    <option> Puerto Rico</option>
    <option> South Africa</option>
    <option> United Kingdom</option>
</select>
Timothy Alexis Vass
  • 2,526
  • 2
  • 11
  • 30
  • 2
    Not the best idea, emojis aren't supported on all platforms and with all fonts (even right know in my browser they display as country codes) – mi1000 Apr 23 '23 at 21:10