The selected attribute is a boolean attribute; its presence sets the value of the associated DOM property to true . If the attribute is missing, the value of the selected property is false .
If the parameter has a selected attribute , then the first time the page is loaded or in the form in which the control is located, reset, this parameter will be selected.
If the property parameter is set to true , then this parameter will be selected. However, if the form is reset, the default option will be selected (i.e. the one that has the selected attribute , or the first option, or none).
To set the selected attribute (i.e. select the default option):
var select = document.getElementById('countryselect'); var option; for (var i=0, i<select.options.length; i<iLen; i++) { option = select.options[i]; if (option.value == '4') {
Please note that this may not make the parameter currently selected, it will simply add the selected attribute. To ensure that it is selected (if that is what is required), also set the selected property to true (see below).
Note that the second argument to setAttribute must be a string that is used to set the attribute value. However, the selected attribute does not have a βcustomβ value, so the second argument is ignored (for example, even false still sets the attribute and makes the default parameter selected). This causes some confusion. :-)
To set the selected property (i.e. just make the option the currently selected option):
var select = document.getElementById('countryselect'); var option; for (var i=0, i<select.options.length; i<iLen; i++) { option = select.options[i]; if (option.value == '4') {