PDA

View Full Version : Auto-Set drop down box value


jwoods7
11-02-2002, 09:30 PM
I have a variable stored in a page, and when the page loads, I want it to run a function that automatically sets the dropdown box to that value. Why won't either of these simple functions work...perhaps too simple??

Variable is named Dmonth;
Form is named dateselect;

<script>
function setDate(){
document.dateselect.Dmonth.value = Dmonth
}
</script>

or

<script>
function setDate(){
document.dateselect[document.dateselect.Dmonth.selectedIndex].value = Dmonth
}
</script>

beetle
11-02-2002, 09:35 PM
The 'value' of a SELECT element is only something that is read, not set, since it is actually based on the value of the option that is selected.

You need to loop through the options and when you find the correct value, select that option.function setDate() {
var s = document.dateselect.dmonth;
for (var i=0; i<s.length; i++)
if (s[i].value == Dmonth) {
s[i].selected = true;
break;
}
}Not debugged, but *should* work. :D

jwoods7
11-02-2002, 09:40 PM
Thank you for the prompt reply. I'm getting a javascript error with that code: "s has no properties"

would this line:
var s = document.dateselect.dmonth;

need anything added/changed???

beetle
11-02-2002, 11:01 PM
Oops...look at it closely compared to your code....Javascript is case-sensitve to the strictest degree

var s = document.dateselect.Dmonth;