Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set the Selected Option

Status
Not open for further replies.

wood

MIS
Aug 3, 2000
139
CA
I need to know how to set the selected option in a dropdown box in both Netscape and IE.

I have a script that gets the current day, month, and year. Then I want to set the dropdown boxes to the current date. The option boxes have all the available Months, Days, and Years. There are three seperate boxes.

Does anyone have an idea? Your help is appreciated.
 
Although I do not know enough for this, I think it would be a lot better to use a server-side script to print the tags, thus making the page work the same way in almost all browsers.

If you really want to use javascript, though, the script I conjured up is this:

Code:
<form name=&quot;myForm&quot;>
<select name=&quot;myMonth&quot;>
<option value=&quot;jan&quot;>January</option>
<option value=&quot;feb&quot;>February</option>
<option value=&quot;mar&quot;>March</option>
<option value=&quot;apr&quot;>April</option>
<option value=&quot;may&quot;>May</option>
<option value=&quot;jun&quot;>June</option>
<option value=&quot;jul&quot;>July</option>
<option value=&quot;aug&quot;>August</option>
<option value=&quot;sep&quot;>September</option>
<option value=&quot;oct&quot;>October</option>
<option value=&quot;nov&quot;>November</option>
<option value=&quot;dec&quot;>December</option>
</select>
<select name=&quot;myDay&quot;>
<option value=&quot;1&quot;>1</option>
<option value=&quot;2&quot;>2</option>
<option value=&quot;3&quot;>3</option>
<option value=&quot;4&quot;>4</option>
<option value=&quot;5&quot;>5</option>
<option value=&quot;6&quot;>6</option>
<option value=&quot;7&quot;>7</option>
<option value=&quot;8&quot;>8</option>
<option value=&quot;9&quot;>9</option>
<option value=&quot;10&quot;>10</option>
<option value=&quot;11&quot;>11</option>
<option value=&quot;12&quot;>12</option>
<option value=&quot;13&quot;>13</option>
<option value=&quot;14&quot;>14</option>
<option value=&quot;15&quot;>15</option>
<option value=&quot;16&quot;>16</option>
<option value=&quot;17&quot;>17</option>
<option value=&quot;18&quot;>18</option>
<option value=&quot;19&quot;>19</option>
<option value=&quot;20&quot;>20</option>
<option value=&quot;21&quot;>21</option>
<option value=&quot;22&quot;>22</option>
<option value=&quot;23&quot;>23</option>
<option value=&quot;24&quot;>24</option>
<option value=&quot;25&quot;>25</option>
<option value=&quot;26&quot;>26</option>
<option value=&quot;27&quot;>27</option>
<option value=&quot;28&quot;>28</option>
<option value=&quot;29&quot;>29</option>
<option value=&quot;30&quot;>30</option>
<option value=&quot;31&quot;>31</option>
</select>
<select name=&quot;myYear&quot;>
<option value=&quot;1985&quot;>1985</option>
<option value=&quot;1986&quot;>1986</option>
<option value=&quot;1987&quot;>1987</option>
<option value=&quot;1988&quot;>1988</option>
<option value=&quot;1989&quot;>1989</option>
<option value=&quot;1990&quot;>1990</option>
<option value=&quot;1991&quot;>1991</option>
<option value=&quot;1992&quot;>1992</option>
<option value=&quot;1993&quot;>1993</option>
<option value=&quot;1994&quot;>1994</option>
<option value=&quot;1995&quot;>1995</option>
<option value=&quot;1996&quot;>1996</option>
<option value=&quot;1997&quot;>1997</option>
<option value=&quot;1998&quot;>1998</option>
<option value=&quot;1999&quot;>1999</option>
<option value=&quot;2000&quot;>2000</option>
<option value=&quot;2001&quot;>2001</option>
<option value=&quot;2002&quot;>2002</option>
<option value=&quot;2003&quot;>2003</option>
<option value=&quot;2004&quot;>2004</option>
<option value=&quot;2005&quot;>2005</option>
<option value=&quot;2006&quot;>2006</option>
<option value=&quot;2007&quot;>2007</option>
<option value=&quot;2008&quot;>2008</option>
<option value=&quot;2009&quot;>2009</option>
<option value=&quot;2010&quot;>2010</option>
<option value=&quot;2011&quot;>2011</option>
<option value=&quot;2012&quot;>2012</option>
<option value=&quot;2013&quot;>2013</option>
<option value=&quot;2014&quot;>2014</option>
</select>
</form>

<script type=&quot;text/javascript&quot;>
<!--
var f=document.forms['myForm'];
var d=new Date;
var firstYear=1985;
f['myMonth'].options[d.getMonth()].selected=true;
f['myDay'].options[d.getDate()-1].selected=true;
f['myYear'].options[d.getYear()-firstYear].selected=true;
// -->
</script>

Code:
var firstYear
should be the first year to be in the year select box. This is because
Code:
.options[]
returns the element that is at the position specified, and I don't think you want 2,002 elements in the year select box. In this case,
Code:
firstYear
is 1985, and as you can see if you test this code, the first year displayed in the year select box is 1985.
 
Do you want to set the selected option dynamically, or the default selected option?

Default:
Code:
<option value=&quot;2014&quot; selected>2014</option>
Dynamically:
Code:
document.formname.myYear[index].selected = true;

Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
In order to have the options selected by default (no javascript), you either have to have a CGI script generate the page or you have to change the page each and every day manually. I definitely suggest CGI scripts. Personally, though, I know not enough to be able to create one that will do what you want.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top