Selanit wrote:
Hmm. I wrote a response, but it hasn't appeared - RobG posted while I
was writing it, and I suspect it didn't get posted for that reason.
Anyway, I basically talked about the same things RobG has said - that
add() needs to args, the second being a reference to the option which
the new option will be placed right before, and IE needs that argument
as an integer rather than an object like everybody else. So a code
snippet for inserting a new option into a select element at a specific
position across browsers might look something like this:
var sel = $("TheSelectBox");
var nextOpt = document.all? (code to create an integer for IE) : (code
to get an Option object);
sel.add(new Option("apple",1),nextOpt);
And if you want it at the end of the list, nextOpt should be null.
That is probably the worst solution, using document.all to detect IE
has been deprecated for years and your solution will likely fail in
Opera, which supports document.all but is probably compliant with the
W3C's add method, not IE's.
My problem's solved, then. I can use this to insert new options while
maintaining alphabetical order within the list.
If that is your intention, why not just add it then sort the options?
Alternatively, if you have a fast method of finding the appropriate
index, you can use something like:
var idx = indexToInsertOptionAt;
if (sel.options[idx]) {
sel.insertBefore(newOption, sel.options[idx]);
} else {
sel.appendChild(newOption);
}
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---