I am trying to develop code that is reasonably cross-browser and
supports all significant browsers. I do consider Opera to be a
significant browser and a trend setter. This select problem limits
what I can do for Opera users. Please make it a priority to get this
problem fixed.
I can provide one additional code sample illustrating a related
problem involving select w. optgroups.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Select Load</title>
</head>
<body>
<form id="myForm"></form>
<script type="text/javascript">
var myData = [ 'a', 'b', 'c', 'd', 'e' ];
var mySelect = document.createElement('select');
var emptyOpt = document.createElement('option');
emptyOpt.value = '';
emptyOpt.innerHTML = 'Choose...';
mySelect.appendChild(emptyOpt);
for (var i = 0; i < myData.length; i++) {
var opt = document.createElement('option');
opt.value = myData[i];
opt.innerHTML = myData[i];
if('c' == myData[i]){
opt.selected = true;
}
mySelect.appendChild(opt);
}
document.getElementById('myForm').appendChild(mySelect);
</script>
</body>
</html>
> The following example illustrates how Opera does not properly handle
> the selection of an option within a select that has been dynamically
> generated. This example works properly in IE, FF and Safari.
>
> var opt = document.createElement('option');
> opt.value = myData[i];
> opt.innerHTML = myData[i];
> if('c' == myData[i]){
> opt.selected = true;
> }
> mySelect.appendChild(opt);
The problem seems to be that the state of the form control cannot be set
until it is added to the document. Moving your opt.selected code to after
the appendChild results in what you want. Alternatively, you can leave
your opt.selected right where it is and set opt.defaultSelected to true at
the same time. That also results in what you want.
Please report it to Opera <http://www.opera.com/support/bugs/> (post just
the bug number back here for future reference), as the fewer
implementation differences with other browsers, the better.
--
Andrew Gregory
<URL: http://www.scss.com.au/family/andrew/ >
Thanks Andrew,
I had already posted it to the Opera support bug reporter, although I
didn't save the bug number. They do have my email address.
I appreciate the solution. I felt sick when it looked like I was going
to have to exclude Opera from the target group of browsers.
-G