GeeCON Prague 2014*
The moose likes HTML, CSS and JavaScript and the fly likes What's Wrong With My Select.add() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "What Watch "What New topic
Author

What's Wrong With My Select.add()

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
I keep receiving type mismatch exception from my Select.add() call. What's wrong with it? Is this function supported by IE6?

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
there is no [edit]need for the[/edit] add(), the line ...[....options.length]= newOpt; adds it.

Eric
[ January 18, 2007: Message edited by: Eric Pascarello ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

In IE, the add() method for the select element doesn't follow the W3C rules. You may need to tinker with the parameters that you pass to it to get it to work.

What is the specific error that you are getting, and what exactly are you trying to do?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
You mean IE6 doesn't support Select.add()? But I do get such a function in ie6 by:



And HTMLSelectElement interface of DOM Level2 does define such a method. I get the type mismatch problem when using such a method rather than exception related to undefined method.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Originally posted by Alec Lee:
You mean IE6 doesn't support Select.add()?


Yes, it does. But it's weird about how it interprets the 2nd parameter.

For example, if you are trying to add the option to the end of the list, the W3C Specification states that you should pass null. But IE6 requires a zero. Stuff like that.
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Thanks,

I am looking for a way to insert a new option at the START of the option list. It seems that if I want to directly modify the options[] array, I can only append an element at the end of it by 'options[options.length]=new Option(..)' or overwrite another option by 'options[0]=sth'. This codes result in typeerror exception:


BTW, where can I find the microsoft's spec on its select.add() method?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Have you tried using 0 or 1, rather than an option reference, as the 2nd param?

It's been a while since I've played with this, but I remeber having to branch the code to keep IE happy.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Ah so you want to put it at the top! Yes you need to use add. Guess I miss that when I just wake up the night after a release.



W3C standards wat requires the option reference while IE needs a number. [edit] and yes Bear was right that you have to use a number with IE [/edit]

Eric
[ January 18, 2007: Message edited by: Eric Pascarello ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

I just did some testing and it appears that specifying 0 for IE does what you wish.

So I was remembering incorrectly earlier. To get it at the end of the list, you omit the 2nd param.

Note that this is only for IE. In real browsers, you follow the W3C Spec and use an element reference or null.
 
GeeCON Prague 2014
 
subject: What's Wrong With My Select.add()