Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using JavaScript to set Select Option as Selected (bug)

159 views
Skip to first unread message

Gerard Vignes

unread,
Feb 22, 2008, 2:25:05 PM2/22/08
to
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.

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>

Andrew Gregory

unread,
Feb 22, 2008, 8:58:34 PM2/22/08
to
On Sat, 23 Feb 2008 04:25:05 +0900, Gerard Vignes <gerard...@gmail.com>
wrote:

> 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/ >

Gerard Vignes

unread,
Feb 23, 2008, 3:26:50 PM2/23/08
to
On Feb 22, 5:58 pm, "Andrew Gregory"
<and...@no.spam.scss.com.au.invalid> wrote:
> On Sat, 23 Feb 2008 04:25:05 +0900, Gerard Vignes <gerardvig...@gmail.com>

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

0 new messages