smartsims
asked on
Opera javascript problem with dynamically adding options to a select
Have a problem in Opera 8 beta. Showing up in adding Option objects to a Select object 's options array. It seems to be adding it successfully to the array, however at the end , I get a options.length of 0. The same code works perfectly in IE 6 (XP), and Firefox 1.0 (XP and Mac). Have an another problem in the way of checking for this on Safari..
I can't see why this wouldn't work. Everything else works in Opera apart from this (ie when I generate options as html). The only thing I can think that I might try is to use the Node approach with a createChild / appendChild method (my brain is too wasted to look up the syntax) or just load Opera 7 and see if it is a beta problem.
I'd appreciate someone else having a look at this.
Test code. listPaneName and selectName are just id's. dataArr is a new Array with a simple set of strings appended. Selected is a string of the currently selected value in dataArr.
function LoadSelectionSimple(listPa neName, selectName, dataArr, selected)
{
var rightpane = top.frames["RightPane"];
if (rightpane != null)
{
var listpane = rightpane.frames[listPaneN ame];
if (listpane != null)
{
var selection = listpane.document.getEleme ntById(sel ectName);
if (selection != null)
{
if (dataArr == null)
alert('LoadSelectionSimple : dataArr is null');
else if (dataArr.length == 0)
alert('LoadSelectionSimple : dataArr is empty');
else
{
try
{
for (i = 0; i < dataArr.length; i++)
{
var str = "";
var line = dataArr[i];
var isselected = false;
if (selected != null && line == selected)
isselected = true;
selection.options[selectio n.options. length]
= new Option(line, line, isselected, isselected);
}
}
catch(e)
{
alert('oops-' + e);
}
alert(selection.options.le ngth);
}
}
else
alert('LoadSelectionSimple : no selection');
}
else
alert('LoadSelectionSimple : no listPane');
}
else
alert('LoadSelectionSimple : no rightPane');
};
I can't see why this wouldn't work. Everything else works in Opera apart from this (ie when I generate options as html). The only thing I can think that I might try is to use the Node approach with a createChild / appendChild method (my brain is too wasted to look up the syntax) or just load Opera 7 and see if it is a beta problem.
I'd appreciate someone else having a look at this.
Test code. listPaneName and selectName are just id's. dataArr is a new Array with a simple set of strings appended. Selected is a string of the currently selected value in dataArr.
function LoadSelectionSimple(listPa
{
var rightpane = top.frames["RightPane"];
if (rightpane != null)
{
var listpane = rightpane.frames[listPaneN
if (listpane != null)
{
var selection = listpane.document.getEleme
if (selection != null)
{
if (dataArr == null)
alert('LoadSelectionSimple
else if (dataArr.length == 0)
alert('LoadSelectionSimple
else
{
try
{
for (i = 0; i < dataArr.length; i++)
{
var str = "";
var line = dataArr[i];
var isselected = false;
if (selected != null && line == selected)
isselected = true;
selection.options[selectio
= new Option(line, line, isselected, isselected);
}
}
catch(e)
{
alert('oops-' + e);
}
alert(selection.options.le
}
}
else
alert('LoadSelectionSimple
}
else
alert('LoadSelectionSimple
}
else
alert('LoadSelectionSimple
};
ASKER
Interesting... did you add using the Select.options[] or using another method...
Currently I just keep getting more puzzled, the options array is present, and has the right kind of data type:- OptionsHTMLCollection. Either I'm managing to add to an implicit copy or there is an object going AWOL
Currently I just keep getting more puzzled, the options array is present, and has the right kind of data type:- OptionsHTMLCollection. Either I'm managing to add to an implicit copy or there is an object going AWOL
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ummm - well you helped prove that I'm actually talking to the correct object... The visible object drop down increased from the single HTML option saying 'Junk' with the following code, to having two rows. But the new one had blank text.
try
{
var sel = listpane.document.getEleme ntById("Li stFilter") ;
// sel.options[sel.options.le ngth] = new Option("javascript","javas cript");
sel.options.length++;
sel.options[sel.options.le ngth-1] = new Option("javascript","javas cript");
}
catch(e)
{
alert('oops-' + e);
}
The only other thing I can think of is that I'm calling from one frame to another. The ListPane frame body has a onload=LoadSelectionSimple (listPaneN ame,....)
I wonder if it isn't accepting a new object created in one frame being added to a collection from another frame. Or that I don't have that Select in a Form...
Anyway dawn is approaching - time for bed...
try
{
var sel = listpane.document.getEleme
// sel.options[sel.options.le
sel.options.length++;
sel.options[sel.options.le
}
catch(e)
{
alert('oops-' + e);
}
The only other thing I can think of is that I'm calling from one frame to another. The ListPane frame body has a onload=LoadSelectionSimple
I wonder if it isn't accepting a new object created in one frame being added to a collection from another frame. Or that I don't have that Select in a Form...
Anyway dawn is approaching - time for bed...
ASKER
Time to ratchet up the points
U have ur site accessible online?
if not post ur view sourcE?
if not post ur view sourcE?
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: archrajan & mplungjan
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
cem_turk
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Split: archrajan & mplungjan
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
cem_turk
EE Cleanup Volunteer
and tried some code which adds options to select box dynamicalluy... it works fine and thats not a problem with opera...