Connecting Tech Pros Worldwide Forums | Help | Site Map

Opera - Problem determening the selectedIndex via JavaScript/DOM

Reinhold Schrecker
Guest
 
Posts: n/a
#1: Jun 7 '06
Hi there,

I have a problem determening the preselection of a dropdown list via
JavaScript DOM:
The followin code works fine with IE and Mozilla but does not preselect
the correct element in Opera:

//snip
myCurrentElement =
window.document.getElementById(''role'');
myNewElement =
window.document.getElementsByTagName(''font'')[0];
myNewChildElement =
window.document.getElementsByTagName(''select'')[0];

for (var i = 0; i < optionArray.length; i++)
{
myNewGrandChildElement =
window.document.createElement(''option'');
myNewGrandChildElement.setAttribute(''value'',
optionArray[i]["value"]);
if (optionArray[i]["selected"]!=null)
{

//myNewGrandChildElement.setAttribute(''selected'', ''selected'');
}
myNewGrandChildElementText =
window.document.createTextNode(optionArray[i]["label"]);

myNewGrandChildElement.appendChild(myNewGrandChild ElementText);
myNewChildElement.appendChild(myNewGrandChildEleme nt);
}
myNewElement.appendChild(myNewChildElement);
myCurrentElement.appendChild(myNewElement);
//snip

Regards
Reinhold Schrecker


Martin Honnen
Guest
 
Posts: n/a
#2: Jun 7 '06

re: Opera - Problem determening the selectedIndex via JavaScript/DOM




Reinhold Schrecker wrote:
[color=blue]
> myNewGrandChildElement =
> window.document.createElement(''option'');[/color]
^^ ^^
That seems to be a syntax error.
Anyway, if you want to set the selected attribute of a newly created
option element then do e.g.
newOption.defaultSelected = newOption.selected = true;


--

Martin Honnen
http://JavaScript.FAQTs.com/
Reinhold Schrecker
Guest
 
Posts: n/a
#3: Jun 7 '06

re: Opera - Problem determening the selectedIndex via JavaScript/DOM


Martin Honnen schrieb:
[color=blue][color=green]
> > myNewGrandChildElement =
> > window.document.createElement(''option'');[/color]
> ^^ ^^
> That seems to be a syntax error.
> Anyway, if you want to set the selected attribute of a newly created
> option element then do e.g.
> newOption.defaultSelected = newOption.selected = true;
>
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/[/color]

Sorry, that was my mistake (I'm a newbie in this NG): The Code is
generated by PL/SQL, so I needed '' to get '.
[color=blue]
> Anyway, if you want to set the selected attribute of a newly created
> option element then do e.g.
> newOption.defaultSelected = newOption.selected = true;[/color]


Thanks Martin for your quick assistance, but defaultSelected does not
work either.

Meanwhile I have tried the following code (this time with correct
quoting):

//snip
myCurrentElement = window.document.getElementById('role');
myNewElement =
window.document.getElementsByTagName('font')[0];
myNewChildElement =
window.document.getElementsByTagName('select')[0];

for (var i = 0; i < optionArray.length; i++)
{
myNewGrandChildElement =
window.document.createElement('option');
myNewGrandChildElement.setAttribute('value',
optionArray[i]["value"]);
if (optionArray[i]["selected"]!=null)
{

//myNewGrandChildElement.setAttribute('selected', 'selected');
//myNewGrandChildElement.defaultSelected =
myNewGrandChildElement.selected = true;
myNewChildElement.selectedIndex = i;

}
myNewGrandChildElementText =
window.document.createTextNode(optionArray[i]["label"]);

myNewGrandChildElement.appendChild(myNewGrandChild ElementText);
myNewChildElement.appendChild(myNewGrandChildEleme nt);
}
myNewElement.appendChild(myNewChildElement);
myCurrentElement.appendChild(myNewElement);
window.document.close;
//snip

The funny thing is, that that Opera (V8.54) understands that code if I
replace "i" with a static number like 1, but it does not work with the
variable itself.

Regards
Reinhold

Closed Thread