No, I meant the PHP which outputs the select element and its options. But I did say (emphasis added):
If your page is outputting the HTML via PHP, have PHP add the selected attribute to the item that should be selected.
So either you have other PHP code that does this (other than that in your posts so far), or you don't. If not, you should consider using it, as it is more certain (users may turn off javascript) and more secure (users may easily hack your javascript).
However, it doesn't sound like security is much of an issue here - for just selecting an option element in a select element - though it may be. Certainly though certainty may be an issue - otherwise why would you want to have this happen in the first place?
That said, there are a number of ways to select an option onload using javascript, here's one:
Code:
window.onload = function(){
var f = document.forms.formName, s = f.elements.selectName;
s.options[optionNumber].selected = true;
};
The two highlighted names should be hard coded as the actual name of the form and the actual name of the select element on the page. The highlighted and red number should be replaced with a PHP token that will resolve the option number (numbered 0 to whatever) of the option you want to be selected.
Bookmarks