Click to See Complete Forum and Search --> : Javascript dropdown box
jrthor2
01-07-2003, 08:09 AM
I have a dropdown box right now that looks like this:
<select name="year" size="1">
<option value="02">2002</option>
<option value="03">2003</option>
<option value="04">2004</option>
<option value="05">2005</option>
<option value="06">2006</option>
</select>
Right now, 2002 is what it defaults to because it s the first item in the list, but what I need to do is have the current year "selected", but keep the values as they are, only 2 digits. Can someone help me here. I am very new to javascript.
Thanks!!
khalidali63
01-07-2003, 08:22 AM
Code segment below will do just that.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript1.2">
window.onload = preSelectListItem;
function preSelectListItem(){
var listYr = document.listform.year;
listYr.selectedIndex = 0;//change this value to make any other item preselected
}
</script>
</head>
<body>
<form name="listform">
<select name="year" size="1" onchange="processListBox();">
<option value="02">2002</option>
<option value="03">2003</option>
<option value="04">2004</option>
<option value="05">2005</option>
<option value="06">2006</option>
</select>
</form>
</body>
</html>
jrthor2
01-07-2003, 08:28 AM
Will this change it automatically when the year changes? That's what I need.
Thanks!
khalidali63
01-07-2003, 08:33 AM
nope.
It will change manually.
for that you need a bit more code.
good luck
Khalid
jrthor2
01-07-2003, 08:36 AM
Thanks, but I need it to do it automatically. Can someone please help?
khalidali63
01-07-2003, 08:45 AM
Oh well,
here copy this entire code on the page u already have.all the contents and save.
<html>
<head>
<title>Untitled</title>
<script language="JavaScript1.2">
window.onload = preSelectListItem;
function preSelectListItem(){
var date = new Date();
var curYear = (date.getYear()).toString();
var listYr = document.listform.year;
for(x=0;x<listYr.length;x++){
if(listYr[x].value==curYear){
listYr.selectedIndex = x;
}
}
}
</script>
</head>
<body>
<form name="listform">
<select name="year" size="1" onchange="processListBox();">
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
</select>
</form>
</body>
</html>
jrthor2
01-07-2003, 08:46 AM
ok, but the value can only be 2 digits. Is that posible??
khalidali63
01-07-2003, 08:49 AM
yep..but you have read about string manipulation and or the date function.or I can do it later in the day..take care..gotta run
jrthor2
01-07-2003, 08:51 AM
thanks, I can wait utill you can help me. Hopefully as soon as posible.
thanks a million!!!
jrthor2
01-07-2003, 09:17 AM
I got it working with the following code:
<head>
<html>
<title>Untitled Document</title>
<script language="JavaScript1.2">
window.onload = preSelectListItemYear;
function preSelectListItemYear(){
var date = new Date();
var curYear = (date.getYear()).toString();
if(curYear >= 2000)
{
curYear -= 2000;
}
if(curYear >= 100)
{
curYear -= 100;
}
var listYr = document.test.year;
for(x=0;x<listYr.length;x++){
if(listYr[x].value==curYear){
listYr.selectedIndex = x;
}
}
}
</script>
</head>
<body>
<select name="year" size="1" onchange="processListBox();">
<option value="02">2002</option>
<option value="03">2003</option>
<option value="04">2004</option>
<option value="05">2005</option>
<option value="06">2006</option>
</select>
Thanks for all your help!!