By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 2,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

keydown listener for div element and "event forwarding"

P: n/a
I'm trying to "detect" a keydown event in a DIV.

The idea have a keydown listener attached to document and forward the
event to the div. (I've searched the web, but it was impossible to
handle the noise on this topic.)

I've tried a simple prototype in Firefox and it seems to work.

function forward(e, elem) {
var f, p;
f = document.createEvent("Event");
f.initEvent(e.type, true, true);
for(p in e) {
if(typeof f[p] === "undefined") {
f[p] = e[p];
}
}
elem.dispatchEvent(f);
}

Three questions remain:
(a) is this whole approach crap?

(b) can keydown trigger a listener attached to the div directly (quite a
few oldish sources on google say so, I couldn't confirm that, and the
official mozilla documentation is rather sparse on that)?

(c) did I overlook something serious in my forward function?

Gregor
Oct 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gregor Kofler wrote:
I'm trying to "detect" a keydown event in a DIV.

The idea have a keydown listener attached to document and forward the
event to the div. (I've searched the web, but it was impossible to
handle the noise on this topic.)

I've tried a simple prototype in Firefox and it seems to work.

function forward(e, elem) {
var f, p;
f = document.createEvent("Event");
f.initEvent(e.type, true, true);
for(p in e) {
if(typeof f[p] === "undefined") {
f[p] = e[p];
}
}
elem.dispatchEvent(f);
}

Three questions remain:
(a) is this whole approach crap?
Dunno, createEvent isn't supported by at least one popular browser so if
you care about 60~80% of web traffic, it might be. Also, there are
vaguaries with calling createEvent - even different versions of Mozilla
behave differently. So don't use it unless you are targetting a
specific browser that supports it.

(b) can keydown trigger a listener attached to the div directly (quite a
few oldish sources on google say so, I couldn't confirm that, and the
official mozilla documentation is rather sparse on that)?
Mozilla? Hardly an authority on HTML - the W3C HTML 4.01 is:
<UR: http://www.w3.org/TR/html4/struct/global.html#edef-DIV >
It says the div element supports onkeydown, so you should have no
problems in conforming browsers.

(c) did I overlook something serious in my forward function?
That it's not necessary? :-)
--
Rob
Oct 27 '08 #2

P: n/a
RobG meinte:
>Three questions remain:
(a) is this whole approach crap?

Dunno, createEvent isn't supported by at least one popular browser so if
you care about 60~80% of web traffic, it might be. Also, there are
vaguaries with calling createEvent - even different versions of Mozilla
behave differently. So don't use it unless you are targetting a
specific browser that supports it.
I know. It's a "prototype". My library handles the different models.
>(b) can keydown trigger a listener attached to the div directly (quite
a few oldish sources on google say so, I couldn't confirm that, and
the official mozilla documentation is rather sparse on that)?
<UR: http://www.w3.org/TR/html4/struct/global.html#edef-DIV >

It says the div element supports onkeydown, so you should have no
problems in conforming browsers.
Sort of. If you fire a "keydown" onto the div or one comes bubbling
along from an input, it works. But just
clicking-on-a-div-and-hitting-a-key doesn't evoke any reaction in my
standard keydown listener.
>(c) did I overlook something serious in my forward function?

That it's not necessary? :-)
Nice try. ;-) I'd be happier without it, too. Though I'm not convinced
that I can solve it so easily.

Gregor
Oct 27 '08 #3

P: n/a
Gregor Kofler wrote:
I'm trying to "detect" a keydown event in a DIV.
(b) can keydown trigger a listener attached to the div directly
Yes, give the div a tabindex="0" and it becomes focussable and can
receive key events in IE, Mozilla, Opera.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 27 '08 #4

P: n/a
Martin Honnen meinte:
Gregor Kofler wrote:
>I'm trying to "detect" a keydown event in a DIV.

>(b) can keydown trigger a listener attached to the div directly

Yes, give the div a tabindex="0" and it becomes focussable and can
receive key events in IE, Mozilla, Opera.
Thanks. That's what I'd been looking for. Is there any documentation for
this ..er... bizarre workaround?

Gregor
Oct 27 '08 #5

P: n/a
Gregor Kofler wrote:
Thanks. That's what I'd been looking for. Is there any documentation for
this ..er... bizarre workaround?
I am sure MSDN documents that for IE, other browsers followed and I
think HTML 5 respectively whatwg documents (or rather specifies) that
somewhere: http://www.whatwg.org/specs/web-apps...nt-work/#focus

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 27 '08 #6

P: n/a
Gregor Kofler wrote:
Martin Honnen meinte:
>Gregor Kofler wrote:
>>I'm trying to "detect" a keydown event in a DIV.

>>(b) can keydown trigger a listener attached to the div directly

Yes, give the div a tabindex="0" and it becomes focussable and can
receive key events in IE, Mozilla, Opera.

Thanks. That's what I'd been looking for. Is there any documentation for
this ..er... bizarre workaround?
Yes on MSDN and developer.mozilla.org.

The one problem I've found is that I couldn't can't get the onclick to
fire on Enter press on an arbitrary element.

Garrett
Gregor

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
Oct 30 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.