Enhanced country selection with Css and Javascript

How many times you had to choose your country in a form, with a old, simple popup?

Time to change the way you choose your country (or anything else) with a bit of Css and Javascript.

I am showing the example in a iframe not to mess with WordPress styles.

As you can see, when you move the mouse over the text input, a list with (almost) every world country and flag appears, and when you click on a country, its ISO code is displayed into the text input.
When you move the mouse out of the country list, it closes.

How can it be made?

Let' see the content of the main page

HTML:
  1. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  2. <style type="text/css">
  3. body{margin:0px;font-family:verdana;font-size:11px;background-color:#e5e5e5}
  4. a{
  5.     position:relative;
  6.     z-index:24;
  7. }
  8. a:hover{
  9.     z-index: 25;
  10.     background-color: #ff0}
  11. a span{
  12.     display: none
  13. }
  14. a:hover span{
  15.     display: block;
  16.     position:absolute;
  17.     top: 10px;
  18.     left: 20px;
  19.     width: 200px;
  20.     border: 1px solid #000000;
  21. }
  22.  
  23. input{
  24.     width:30px;
  25.     font-family:verdana
  26. }
  27.  
  28.     <form id="countrysel" method="get" name="countrysel">
  29.         Select country: <a href="javascript:void(0)"><input type="text" name="country" readonly><span><iframe src="index.html" frameborder="0" scrolling="yes" height="150" width="220"></iframe></span></a>
  30.     </form>
  31.     other content here other content here other content here

The trick is in the <a> and <span> css use, with a display attribute changing on hover status.

It's easiest to see than to explain.

Let's look at line 34, when I call the iframe I simply load an ordered list with a javascript to communicate with the "parent" page.

HTML:
  1. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  2. <style type="text/css">
  3. body{margin:0px;background-color: #000000}
  4. ul{margin:0px;padding:0px;margin-left: 1px;padding-left: 0;list-style-type: none;font-family: verdana;font-size:11px;text-transform: capitalize;}
  5. img{ vertical-align: middle}
  6. a{display: block;width: 200px;background-color: #f5f5f5;padding:4px;}
  7. a:link, a:visited{color: #000000;text-decoration: none;}
  8. a:hover{background-color: #ffaf2e;color: #000000;font-weight:bold;}
  9. <li><a href = "javascript:void(0)" onclick = "parent.document.countrysel.country.value = 'af'"><img src="af.gif" alt="" width="16" height="11" border="0" /> afghanistan</a>
  10. <li><a href = "javascript:void(0)" onclick = "parent.document.countrysel.country.value = 'ax'"><img src="ax.gif" alt="" width="16" height="11" border="0" /> &aring;land islands</a>
  11. <li><a href = "javascript:void(0)" onclick = "parent.document.countrysel.country.value = 'al'"><img src="al.gif" alt="" width="16" height="11" border="0" /> albania</a>
  12. ...
  13. ...

Look how I set the menu starting with an unordered list with Css (lines 5-10) and how I pass ISO values to the parent page (line 15 for example).

You can download the whole work here, check 2 html files and give me feedback or suggestion about the use of this feature.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.4 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Template Monster offers you a great assortment of CSS Templates.
They can be a great start to launch a website of your own and meet the individual needs of your project.

12 Responses to “Enhanced country selection with Css and Javascript”

  1. Spiketmr on August 1st, 2006 9:01 pm

    Great script. Thanks alot.

  2. Ajay on August 18th, 2006 10:39 pm

    Gr8 work…Can i use it..in my commercial website..without much modification in the code.

  3. Emanuele Feronato on August 18th, 2006 11:03 pm

    Of course… and do not forgeto to show me the final result

  4. metz on September 26th, 2006 7:34 pm

    Bugs like a hillbilly in Opera.

  5. saeed on January 1st, 2007 1:51 pm

    its very intersted

  6. Kerim Incedayi on January 2nd, 2007 11:55 pm

    doesnt work with IE7… but IE7 doesnt work much anyways so no worries.. lol :P

  7. Thomas on September 19th, 2007 6:22 am

    it does work in ie7, but ie7 that you used was probably beta…

    amazing! there’s great need for this in almost every website! thanks!

  8. Thomas on September 19th, 2007 6:25 am

    oh … i see what you mean, kerim. when i downloaded it and view it won’t work! sorry about that!

  9. Ste on October 13th, 2007 4:22 am

    Dont work in firefox!!!

  10. Beenish on October 30th, 2007 10:28 am

    Please provide me the source code for this.
    I have a big list of values, which i want
    to display like this.
    Thanks,
    Ben

  11. Kevin on November 22nd, 2007 8:36 pm

    Can you add a state/territory and zip code. Thank you

  12. manbros on November 11th, 2008 7:05 am

    Looking for javascript Pakistan cities like world countries.

Leave a Reply




Posts