add() method

L

Luca Berti

Someone can tell me why this code does everything but work under IE (6)???

var newEditor = new Option(document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options.add(newEditor, null);
opener.document.newsform.editor.selectedIndex =
opener.document.newsform.editor.length - 1;
 
K

kaeli

Someone can tell me why this code does everything but work under IE (6)???

var newEditor = new Option(document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options.add(newEditor, null);
opener.document.newsform.editor.selectedIndex =
opener.document.newsform.editor.length - 1;

Not really, since I don't have testable code here, just a fragment with your
assertion that it doesn't "work". I have no idea what you mean by that. If
you took your car to the mechanic, would you tell him that it doesn't work?
Or would you tell him that when you do something, something else happens, and
so on?
How can I know if you properly defined your form elements, if this page even
HAS an opener, and so on?

Anyway, making new options and adding them somewhere...
See my example here:
http://www.ipwebdesign.net/kaelisSpace/useful_dynamicSelects.html

If you want us to help you, you need to post something we can test or a URL
to the complete code. Do NOT post a huge amount of code. Just a small example
that demonstrates the problem in a way we can copy and test. And DO tell us
what you expected to happen, what did happen, and any errors, if any show up.

Half the time, trimming code to a small test version shows you what the error
was, anyway.

--
--
~kaeli~
Jesus saves, Allah protects, and Cthulhu thinks you'd make
a nice sandwich.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Michael Winter

Someone can tell me why this code does everything but work under IE
(6)???

var newEditor = new Option(document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options.add(newEditor, null);
opener.document.newsform.editor.selectedIndex =
opener.document.newsform.editor.length - 1;

The problem here is that Microsoft, for some reason, implemented the DOM
method, HTMLSelectElement.add(), but changed its signature. Instead of the
second argument being a required object reference (or null), it's an
optional ordinal number.

Unfortunately, this means having to cope with two completely incompatible
methods. There are three ways to deal with this problem, but I wouldn't
call any of them attractive:

1) Detect IE and call the relevant method.
2) Use try/catch, trying one set of parameters then the other.
3) Use the older "new Option" approach for all cases.

The first two options are unreliable. A lot of browsers disguise
themselves as IE, so detecting IE with any certainty is impossible. The
use of try/catch depends upon it existing in the first place; they are
fairly recent commands, and there's no real way to check if they can be
used, but this would be the best, otherwise.

The third option, whilst useable, shouldn't (in my opinion) be used for
anything other than a fallback approach for old browsers that don't
support the DOM.

Secondly, whilst this isn't a problem, I wouldn't advise it: mixing two
approaches.

When you use the older "new Option" approach, you add the resulting
element using the HTMLSelectElement.options collection, simply assigning
where you want the option to go.

When you use the newer DOM approach with document.createElement(), you add
the resulting element using HTMLSelectElement.add().

So choose either:

var newEditor = new Option(
document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options[
opener.document.newsform.editor.length] = newEditor;

or:

var sel = opener.document.newsform.editor;

if(document.createElement && sel.add) {
var newEditor = document.createElement('OPTION');

if(newEditor) {
var e;

newEditor.text = newEditor.value =
document.add_editor.nome.value;

try {
sel.add(newEditor, null);
} catch(e) {
sel.add(newEditor);
}
}
}

By the way, you should look into saving object references, rather than
accessing each element using its fully qualified reference. It's more
efficient and makes for a smaller script. For example:

var f = document.forms['formName'];
var e = f.elements['controlName'];

or

var c = document.forms['formName'].elements;
var e = c['controlName'];

Which you choose depends upon whether you need to access properties of the
form, or just its controls.

Mike
 
L

Luca Berti

I implemented your code, but IE continued to traw out exeptions...
Then I copied the code in a function on the page that contains the "SELECT"
element and called that one from the opened window. Now it works.
By the way, you should look into saving object references, rather than
accessing each element using its fully qualified reference. It's more
efficient and makes for a smaller script.

Thx for the advce.
Luca
 
S

Steve van Dongen

Luca Berti said:
Someone can tell me why this code does everything but work under IE (6)???

var newEditor = new Option(document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options.add(newEditor, null);
opener.document.newsform.editor.selectedIndex =
opener.document.newsform.editor.length - 1;

PRB: Problem Adding New Option to Select Box in Another Frame in
Internet Explorer 5
http://support.microsoft.com/?id=237831

SYMPTOMS
In Internet Explore 4.0 it is possible to create a new option in one
frame and add it to a select box in another frame. This functionality
is not supported in Internet Explorer 5.

Using this functionality causes the following Java script error:
Object does not support this property or method

CAUSE
This was not an intended functionality, which caused some instability.
It was removed from Internet Explorer 5.

RESOLUTION
There are two ways to work around this. The first way is call Internet
Explorer 5's new createElement method on the target frame. The second
method is to create a subroutine in the target frame to create the
option and then return a reference to it.

STATUS
This behavior is by design.

Regards,
Steve
 

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

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top