Opera - Problem determening the selectedIndex via JavaScript/DOM

Joined
Feb 4, 2010
Messages
28
Reaction score
0
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["value"]);
if (optionArray["selected"]!=null)
{

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

myNewGrandChildElement.appendChild(myNewGrandChildElementText);
myNewChildElement.appendChild(myNewGrandChildElement);
}
myNewElement.appendChild(myNewChildElement);
myCurrentElement.appendChild(myNewElement);
//snip

Regards
 
Last edited by a moderator:
M

Martin Honnen

> myNewGrandChildElement =
window.document.createElement(''option'');
^^ ^^
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;
 
Last edited by a moderator:
Joined
Feb 4, 2010
Messages
28
Reaction score
0
Martin Honnen schrieb:

> > myNewGrandChildElement =
> > window.document.createElement(''option'');
> ^^ ^^
> 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/


Sorry, that was my mistake (I'm a newbie in this NG): The Code is
generated by PL/SQL, so I needed '' to get '.

> Anyway, if you want to set the selected attribute of a newly created
> option element then do e.g.
> newOption.defaultSelected = newOption.selected = true;



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["value"]);
if (optionArray["selected"]!=null)
{

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

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

myNewGrandChildElement.appendChild(myNewGrandChildElementText);
myNewChildElement.appendChild(myNewGrandChildElement);
}
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
 
Last edited by a moderator:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top