Posted by: prajapatinilesh | September 14, 2007

Dropdown Selected Value Code

Server-side approach

$month = date(“F”);
$months_array = array( “january”, “february”, “march”, “april”, “may”, “june”, “july”, “august”, “september”, “october”, “november”, “december”);
<select id=”month”><?php /* $length = count( $months_array ); */
for ( $i = 0; $i < 11; $i++ ){
if ( $months_array[i] == strtolower($month) )
{ echo “\t<option value=\”” . $months_array[i] . “\” selected=\”selected\”>” . $month . “</option>\n” } else {
echo “\t<option value=\”” . $months_array[i] . “\”>” . ucwords( $months_array[i] ) . “</option>\n”
}
} ?> </select>

JavaScript/DOM approach

var months = new Array(12)
months[0] = “january”;
months[1] = “february”;
months[2] = “march”;
months[3] = “april”;
months[4] = “may”;
months[5] = “june”;
months[6] = “july”;
months[7] = “august”;
months[8] = “september”;
months[9] = “october”;
months[10] = “november”;
months[11] = “december”;
var date_now = new Date();
var month_now = date_now.getMonth();
var monthList = document.getElementById(“month”);
var curMonthOption = document.getElementById( months[month_now] );
curMonthOption.selected = true;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: