Welcome to WebmasterWorld Guest from 3.227.233.55

Forum Moderators: open

Message Too Old, No Replies

options[i].selected javascript bug in NN6?

Assigning select options to an Array

     
11:27 pm on Aug 22, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


I have javascript that works fine in IE5.5. IE6 and NN7, but falls short in NN6.2.2. I'm moving items from one
<select>
list to another, keeping the item selected when it hits the other list, and then sorting the list. All is fine except in NN6.2.2 I lose my option's selected status, and it is happening right before the sort function. In order to sort, I'm moving the
select.option
array into a
new Array()
and it's at that point that the value is returning
false
. Here is a small piece of the javascript for reference:

if (to.name == 'list1') {
sort_list = new Array();
for (var i = 0; i < to.options.length; i++) {
sort_list[i] = new Option(to.options[i].text, to.options[i].value, to.options[i].defaultSelected, to.options[i].selected)
alert('Before: ' + to.options[i].text + "/" + to.options[i].selected + "\nAfter: " + sort_list[i].text + "/" + sort_list[i].selected)
}
.
.
.

Anybody know if there is a bug in NN6? I'm hoping it's a NN6 browser version bug, but I want to doubt that since IE5.5 can process the script correctly. I have a lightweight version of this code (60 lines) that I could post if anyone wants to test it in NN6 for me and help try to figure this out.

[edited by: tedster at 11:51 pm (utc) on Aug. 22, 2003]
[edit reason] reduce side scroll [/edit]

1:26 pm on Aug 23, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 20, 2002
posts:3732
votes: 0


The code looks OK except you are not ending your first statement line with a semicolon. This has been know to cause sporadic errors on some browsers.

if (to.name == 'list1') {
sort_list = new Array(); for (var i = 0; i < to.options.length; i++) {
sort_list[i] = new Option(to.options[i].text, to.options[i].value, to.options[i].defaultSelected, to.options[i].selected);

alert('Before: ' + to.options[i].text + "/" + to.options[i].selected + "\nAfter: " + sort_list[i].text + "/" + sort_list[i].selected);
}
}

1:33 pm on Aug 23, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


You may also want to check that the JS version NN6 uses (1.4, I believe, mabye even 1.3), has the same syntax for the Option constructor -- if it only takes 3 params instead of 4, your to.options[i].selected might be getting send off into oblivion, but you still wouldn't get an error, because the object itself has a 'selected' property. Other than that, and the semicolen issue korkus mentioned, looks like it would be a bug.

========

Ps. If it is the constructor that is the issue, you could just do...

sort_list[i] = new Option();
sort_list[i].text = to.options[i].text;
sort_list[i].value = to.options[i].value;
sort_list[i].defaultSelected = to.options[i].defaultSelected;
sort_list[i].selected = to.options[i].selected;

Jordan

2:06 pm on Aug 23, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


I've tried both suggestions. Adding the semicolons did not make a difference. And I had already thought maybe the parameter list was the previous version, so I tried setting the selection to true using the method you described MonkeeSage. I'm at wits end with this one.
2:27 pm on Aug 23, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


Well, it's a bit of a kludge, but what I think will work is to make a global array to hold selected values...something like...

var select_list = new Array(); 

if (to.name == 'list1') {
sort_list = new Array();
for (var i = 0; i < to.options.length; i++) {
sort_list[i] = new Option(to.options[i].text,
to.options[i].value,
to.options[i].defaultSelected,
to.options[i].selected);
select_list[i] = to.options[i].selected;
alert('Before: ' + to.options[i].text + "/" +
to.options[i].selected + "\nAfter: " +
sort_list[i].text + "/" +
sort_list[i].selected);
}
}

...then after you insert the new list (assuming for the example that it has the id "list"), run through another loop...

var sl = document.getElementById("list"); 
for (var i = 0; i < sl.options.length; ++i) {
sl.options[i].selected = select_list[i];
}

HTH
Jordan

7:46 pm on Aug 25, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


...then after you insert the new list (assuming for the example that it has the id "list"), run through another loop...
Yeah, tried this, too. It won't work as the array has been sorted and no longer has the same sequence at this point. I think it has something to do with the way that NN6 handles a new Array() versus new Option() and/or how I'm attempting to use them that way. All the support I find describes sorting arrays using this technique, including devedge.netscape.com. I was just hoping to find some conclusive evidence that I've really got my code wrong rather than discovering a bug (that has apparently been fixed, since NN7 processes just fine). I'm just concerned about backward compatibility. If I could employ a viable NN6 workaround, I'd be happy.
8:45 am on Aug 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


Ahh, duh, the array members won't map after the one is sorted...hmmm...that is an interesting problem. Not sure of exactly how it could be worked around without adding (quite) a bit of extra bloatage (like another array for a sort map kind of thing). I don't see anything wrong with your code anywhere, so I am betting it really is a bug.

Jordan

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members