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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Value of Drop Down in Form not working

Status
Not open for further replies.

johrik

Programmer
Joined
Feb 9, 2006
Messages
4
Location
GB
Hi all,

I have 3 fields in a form of which 2 are being populated fine with the following syntax:

document.predealform.sdDay.value = sd_start_date;
document.predealform.sdMonth.value = start_date_month;
document.predealform.sdYear.value = start_date_year;

The var on the right hand side all def have values which I prove with an alert box displaying them. For some reason the first and last fields populate but the month one is left blank when loaded?

Any suggestions would me much appreciated,
 
Hi,

Yes its def called sdMonth as below:
<input type="text" name="sdDay" size="1" maxlength="2" >
<select name="sdMonth" size="1">
<option value="01">Jan</option>
<option value=" 02">Feb</option>
</select>
<select name="sdYear" size="1">
<option value="2005">2005</option>
<option value="2006">2006</option>
</select>

Wierd thing is that 2 of the fields populate the month one doesnt. Also it seems to work ok in Firefox and not in IE?
 
Sorry got that slightly wrong, it doesnt work in Firefox just it defaults to the first value in the dropdown...

In IE it doesnt default just goes blank. I want to set it to the value "start_date_month" which def has a value (as the alert box proves this) but it doesnt seem to work,
 
Aaah - I don't believe you can reliably set the selected option in a select element by setting its value. You should set the "selectedIndex" property instead, so:

Code:
document.forms['predealform'].elements['sdMonth'].selectedIndex = 1;

Note that the index numbers start from 0, so in the above example, "1" would select the second item (in your case, "Feb").

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yep thats it!! Thanks for youre help I did the following:

document.forms['predealform'].elements['sdMonth'].selectedIndex = (start_date_month-1)

start_date_month is the variable thats being passed in as a number and it sets the correct date,

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top