Finding the focused element of a form is something that I''ve run into a
few times and usually, I''ve use an onFocus event to set a variable
somewhere within an accessible name space which could be checked, but
while effective, it always seemed like it should be a part of form and
not a random variable. So, I hacked around and came up with this
solution. First, some code:
Form.getElements($("some_form")).invoke("observe",
"focus",
function(event) {
var focused_element =
Form.getElements($("some_form")).find(function(e) { return e.focused
==
true; });
if(!focused_element)
Form.getElements($("some_form")).each(function(el) { el.focused =
false; });
else focused_element.focused = false;
Event.element(event).focused = true;
} Form.focusFirstElement($("some_form"));
The above code will first try to find a focused element. if it can''t,
it will add a focused property to all elements of the form and set it to
false. Otherwise, it''ll unset the focused flag for the element that it
finds. Finally, it sets the focused flag for the element which spawned
the event to true. The first line of the anonymous function can then be
used later to find the focused element. I also use the
Form.focusFirstElement() function to automatically make sure that the
system will (obviously) focus the first element but (perhaps less
obviously) also create a focus event for the element which triggers the
setup of all the code above.
The code seems pretty compact and it''s very effective. I
haven''t looked
up yet to confirm if the Form.getElements function returns extended
elements or not, so IE might be a problem with this specific
implementation. But more that the nitty gritty of browser concerns, I
wonder if anyone has a better way?
- Dash -
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---