This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "What Watch "What New topic
Author

What's Wrong With My Select.add()

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 568
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: 15357
    
    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: 56192
    
  13

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 568
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: 56192
    
  13

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: 568
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: 56192
    
  13

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: 15357
    
    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: 56192
    
  13

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What's Wrong With My Select.add()
 
Similar Threads
list......jsp, javascript
Update select list from a callee page
new option() and &
Passing parameters in XSL to JavaScript
Passing parameters in XSL to JavaScript