> Csaba, please, give me some additional information about the
> function(sel){return function() {
> sel.options[sel.options.length - 1].selected=true;}}(sel)
> How does it work?
Please read the three posts (5-7) starting from Richard Cornford's at:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/94165d8695
faf91d/
and a bit of followup at (the last three posts. 6-8, of):
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bff638ebc5
a4fe21/
In a nutshell, the code of the OP (original poster) mostly did what he
wanted, but when you modify form elements, especially when you are in
their event handlers, the modification might not be immediate (so any
follow on usage should wait a bit), or it might be immediate, but then
subsequent event handling (including default handling) might change it,
so the change should be postponed. In both cases, a window.setTimeout
will usually accomplish the waiting.
window.setTimeout can take a string as the first argument for what to
do, but it's much more cool to use a function instead. When
.setTimeout gets around to invoking the function, it doesn't pass any
arguments (mostly. See part 2 of the second thread for an exception),
but sometimes it is convenient for that function to know about values
that existed when the .setTimeout was called. That's why the inner
function (which is returned as the 1st argument to .setTimeout because
the outer function is evaluated) is wrapped in the outer one - the
inner function will know about the variables that are defined for the
outer function. So all I did was to put the OP's line of code in a(n
inner) function (so that .setTimeout could work with it), but I needed
to wrap that function in an outer function so the that the inner
function would know about the select element, sel, when it ran.