RobG wrote:
> That is probably the worst solution, using document.all to detect IE
> has been deprecated for years ...
True. Usually, I try to test for the specific functionality I need -
eg retrieving the node that an event originated in by testing the
typeof event.srcElement:
(typeof(event.srcElement) == "object")? callerNode = event.srcElement :
callerNode = event.target;
But that approach only works with properties; as far as I know, there's
no good way to test what type of argument a browser is expecting for a
method. I'd be happy to learn of one, though.
> ... 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.
I did test it in Opera 9 before posting, and it works quite nicely.
Opera appears to support both formats for the add method. I don't test
earlier versions of Opera, so I can't say whether it works in those.
> > 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?
I didn't think of it. Also, now that I've tried it, it's not as
straightforward as it sounds.
This code:
var sel = $("TestSelect");
sel.options.sort();
... will throw an error in every browser I've tried it in, 'cause the
options property isn't an array, it's an HTMLOptionsCollection object.
So it needs to be converted into an array first:
var sel = $("TestSelect2");
var arr = $A(sel.options).sort();
So now we've got a sorted array. We can't just assign the whole array
back to options, so we need to truncate the options array and restore
each one from the array.
sel.options.length = 0;
arr.each(function(value, index){ sel.options[index] = value; });
That does in fact rebuild the options in the select box.
Unfortunately, it sorts them according to the option *values* rather
than the option *labels*. In my case, the value is numerical, and the
label is textual, and sort() organizes the list according to the
numerical values. For example:
<option value="3">Apple</option>
<option value="2">Grape</option>
<option value="1">Banana</option>
<option value="11">Kiwi</option>
Given this list, using sort() produces this list:
<option value="1">Banana</option>
<option value="11">Kiwi</option>
<option value="2">Grape</option>
<option value="3">Apple</option>
The end result is not alphabetical order, and is actually more likely
to confuse the user, since one non-alphabetical order has been replaced
with a different non-alphabetical order with no visible rhyme or reason
to it. In order to fix this, we would need to re-format the array
generated by $A($("TestSelect").options) in order to make sort()
operate on the labels instead of the values, and then reformat the
sorted array AGAIN so that each given entry would be in the right order
to get assigned back to the options object as an option element. I
didn't take my experiments far enough to do that, because frankly it
sounds like a pain in the butt.
The method I've been using thus far works like this:
var ID = 6; // This is a SQL insert ID retrieved via an AJAX call.
var Name = "Joe Schmoe"; // This is supplied in the form.
var insert = '<option value="'+ID+'"
id="Account'+ID+'">'+Name+'<\/option>';
var found =
$A($("SelectUser").getElementsByTagName("option")).find(function(item)
{
return Name < item.innerHTML;
});
if (found != null){ new Insertion.Before(found, insert); } else { new
Insertion.Bottom($("SelectUser"), insert); }
And, of course, the Insertion commands are the cause of this whole
thread - they exhibit the problems I initially mentioned in IE and in
Opera. I figure I can rewrite this to use add(). Using a numerical
value for the second argument of add() throws a type mismatch error in
Firefox, so I figure I can use Try.these() and just run it both ways
rather than trying to figure out which the browser wants.
But first I'm going to update to Prototype 1.5 and see if that fixes
the Insertion errors. That would be even better.
For that matter, I don't even know why I'm worrying about IE and Opera.
This particular page is destined to get used by exactly two people: me
and my boss. I use Firefox, she uses Safari. Sometimes diligence is a
curse.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---