Selecting a value in drop down list using javascript
<p><strong>A] Summary of the problem:</strong></p>

<p>Trying to pre-select a users country on page load</p>

<p><strong>B] Details:</strong></p>

<p>1] I am using maximinds javascript to find out a users county. </p>

<p>2] I have a drop down list which contains a list of 225 or so countries.</p>

<p>3] Inside the javascript section of my HTMLr page, I am trying to select the users country from the drop-down list. </p>

<p>But the country is not getting selected</p>

<p><strong>C] Code excerpt:</strong></p>

<pre><code>&lt;select name="country_name" id="id_country_name"&gt;
 &lt;option value="" selected="selected"&gt;---------&lt;/option&gt;
 &lt;option value="Afghanistan"&gt;Afghanistan&lt;/option&gt;
 &lt;option value="Aring land Islands"&gt;Aring land Islands&lt;/option&gt;
&lt;/select&gt;


&lt;!-- including the  geoip javascript library --&gt;
&lt;script src="http://j.maxmind.com/app/geoip.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;!-- By default, we will select users country --&gt;       
&lt;script type="text/javascript" language="JavaScript"&gt;
document.getElementById(geoip_country_name()).selected = true
&lt;/script&gt;
</code></pre>

<p>Thanks,</p>

<p><strong>[Edit#1]</strong></p>

<p>Tried the following jquery code, but this isnt populating the drop-down list:</p>

<pre><code>&lt;!-- including the  geoip javascript library --&gt;
  &lt;script src="http://j.maxmind.com/app/geoip.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
    $(function () {
        $("option[value='" + geoip_country_name() + "']").attr('selected', 'selected');
    });
 &lt;/script&gt;
</code></pre>

<p>[Edit#2] </p>

<p>Tried the $("#id_country_name").val(geoip_country_name());,  </p>

<pre><code>&lt;div id="userDataForm"&gt;
  &lt;form method="POST" action="/UserReporting"&gt; 
  &lt;table&gt;
      &lt;!-- Printing the forms for users country, city --&gt;
      &lt;tr&gt;&lt;th&gt;&lt;label for="id_country_name"&gt;Country name:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;
      &lt;select name="country_name" id="id_country_name"&gt;
    &lt;option value="" selected="selected"&gt;---------&lt;/option&gt;
    &lt;option value="Afghanistan"&gt;Afghanistan&lt;/option&gt;
        &lt;option value="Aring land Islands"&gt;Aring land Islands&lt;/option&gt;
        &lt;option value="Albania"&gt;Albania&lt;/option&gt;
      &lt;/select&gt;
      &lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;th&gt;
      &lt;label for="id_city_name"&gt;City name:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;input type="text" name="city_name" id="id_city_name" /&gt;&lt;/td&gt;&lt;/tr&gt;     
 &lt;/table&gt;

 &lt;input type="submit" name="report_up" value= "Report Up"&gt;
 &lt;input type="submit" name="report_down" value= "Report Down"&gt;
 &lt;/form&gt;
</code></pre>

<p></p>

<pre><code> &lt;!-- including the  geoip javascript library --&gt;
&lt;script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;
&lt;/script&gt;
&lt;script src="http://j.maxmind.com/app/geoip.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
    $(function () {
        $("#id_country_name").val(geoip_country_name());
    });
&lt;/script&gt;
</code></pre>
