• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

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(listPaneName, selectName, dataArr, selected)
{
      var rightpane = top.frames["RightPane"];
      if (rightpane != null)
      {
            var listpane = rightpane.frames[listPaneName];
            if (listpane != null)
            {
                  var selection = listpane.document.getElementById(selectName);
                  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[selection.options.length]
                                          = new Option(line, line, isselected, isselected);
                              }
                        }
                        catch(e)
                        {
                              alert('oops-' + e);
                        }
                        alert(selection.options.length);
                        }
                  }
                  else
                        alert('LoadSelectionSimple: no selection');
            }
            else
                  alert('LoadSelectionSimple: no listPane');
      }
      else
            alert('LoadSelectionSimple: no rightPane');
};
0
smartsims
Asked:
smartsims
2 Solutions
 
archrajanCommented:
I just downloaded opera 8
and tried some code which adds options to select box dynamicalluy... it works fine and thats not  a problem with opera...

0
 
smartsimsAuthor Commented:
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
0
 
archrajanCommented:
here my syntax:
var sel = document.formname.selectboxname
var opt = new Option("javascript","javascript","javascript");
sel.options[sel.length] = opt;

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Michel PlungjanIT ExpertCommented:
If you run into problems with that, then try

var sel = document.formname.selectboxname
sel.options.length++
sel.options[sel.options.length-1] = new Option("javascript","javascript","javascript");

that is anyway a fix for an older IE problem
0
 
smartsimsAuthor Commented:
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.getElementById("ListFilter");
//                  sel.options[sel.options.length] = new Option("javascript","javascript");
                  sel.options.length++;
                  sel.options[sel.options.length-1] = new Option("javascript","javascript");
                  }
                  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(listPaneName,....)

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...
0
 
smartsimsAuthor Commented:
Time to ratchet up the points
0
 
archrajanCommented:
U have ur site accessible online?
if not post ur view sourcE?
0
 
Cem TürkSenior Software EngineerCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now