Charles Lowell

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromCharles Lowell
ToMe
Subjectmanually firing key events does not appear to trigger the default action
Date23 March 2006 17:00
I followed your instructions at
http://www.howtocreate.co.uk/tutorials/javascript/domevents

to try and simulate hitting the TAB key (keyCode: 9) to advance the focus.

While, the event dispatches with the correct keyCode, and bubbles all
the way up to document element, it does not trigger the default action
which is to advance the focus to the next focusable element. I would
expect this to work in all browsers, but I tested it in firefox1.5,
opera 8.52, konqueror 3.5.1. and internet explorer 6.0

They all properly created a key event that bubbled correctly, but none
of them performed the default action for a TAB key event of advancing focus.

Do you know of anyway to manually fire events that trigger default
actions? Better yet, is there anyway to trigger the advance focus logic
in the browser via a more indirect way?

cheers,
Charles
FromMe
ToCharles Lowell
SubjectRe: manually firing key events does not appear to trigger the default action
Date23 March 2006 17:15
Charles,

> While, the event dispatches with the correct keyCode, and bubbles all
> the way up to document element, it does not trigger the default action
> which is to advance the focus to the next focusable element.

Correct, it just fires the event, it does not actually create the associated
default action. It expects you to do that yourself. In this particular case,
that is quite important, since the default action of TAB is different in
different browsers. In IE for example, it jumps to the next link or form
input. In Opera, it jumps to the next form input (jumping between links is
done with A and Q).

> Do you know of anyway to manually fire events that trigger default
> actions? Better yet, is there anyway to trigger the advance focus logic
> in the browser via a more indirect way?

generally, if you are manually triggering event handlers, you also have to
manually perform the actions as well. This means calling the .focus() method
of the destination form input (assuming it has a focus method).

The focus method is a little special. unlike many similar methods (submit,
for example) it does fire events when it is called. So just call the focus
method, and any required event focus handlers will be run.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromCharles Lowell
ToMe
SubjectRe: manually firing key events does not appear to trigger the default action
Date23 March 2006 19:43
Mark,

> Correct, it just fires the event, it does not actually create the
> associated default action. It expects you to do that yourself. In this
> particular case, that is quite important, since the default action of
> TAB is different in different browsers. In IE for example, it jumps to
> the next link or form input. In Opera, it jumps to the next form input
> (jumping between links is done with A and Q).

Exactly. I want it to do whatever *would* happen on the browser in which
the script is running, whatever that behavior might be. Currently, I am
implementing the action by myself, by going to the next "focusable"
element. The problem is, as you say, that what that element is depends
on the browser, so my code will cause non-standard behavior in some
browsers such as opera. I'm a big fan of having things work as people
would expect them to work in the context of their environments, so what
I'd like to do is say to the browser "do what you would do if someone
hit the TAB" be it to advance to form inputs and links, or in the case
of opera, just advance to form inputs. As it stands, to achieve the
"expected result" I have to reimplement the focus advance logic for each
browser in which it differs. Obviously, the browser vendors have already
written this logic somewhere, and so I'm trying to see if there is an
interface to invoke it, rather than having to rewrite it myself.

[snip.]

Either way, your discussion on how to manually create events was the
best I could find on the internet. This is such a poorly documented
feature. I searched for ages without turning up anything useful and
cross-browser. Thank you  very much.

cheers,
Charles
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.