Preselect first option in dynamically populated dropdown list
<p>I have a dropdown list that is populated from a mySQL database using PHP.  How do I preselect the first option in the dropdown bearing in mind that these values may not always be the same?</p>

<p>This is the code in my HTML:</p>

<pre><code>function loadparameters(selobj, url, nameattr) {
        $(selobj).empty();
        $.getJSON(url, {}, function (data) {
            $.each(data, function (i, obj) {
                $(selobj).append($("&lt;/option&gt;&lt;option&gt;
               &lt;/option&gt;").val(obj[nameattr]).html(obj[nameattr]));
            });
        });
    }

    $(function () {
        loadparameters($('select#parameterType').get(0), 
       'GetParameter.php?GetParameter=parameterType', 'parameterType');});
</code></pre>

<p>..and the PHP Script:</p>

<pre><code>&lt;?php

if(!empty($_GET['GetParameter']))
{
$list = $_GET['GetParameter'];
$qry='';
switch($list)
{
    case 'parameterType':
    {
        $qry = "SELECT parameterType FROM Parameters";
        break;
    }
}

if(empty($qry)){ echo "invalid params! "; exit; }

$dbconn = mysql_connect('*******','*******','******') 
        or die("DB login failed!");

mysql_select_db('*****', $dbconn) 
        or die("Database does not exist! ".mysql_error($dbconn));

$result = mysql_query($qry,$dbconn)
        or die("Query $qry failed! ".mysql_error($dbconn));

$rows = array();

 while($rec = mysql_fetch_assoc($result)) 
{
    $rows[] = $rec;
}
mysql_free_result($result);
mysql_close($dbconn);

echo json_encode($rows);
}
?&gt;
</code></pre>
