473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

keydown listener for div element and "event forwarding"

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
6 7501
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Corepaul | last post by:
I am a newbie using Access 2000. I am using the following test in the txtAlbum OnExit procedure to prevent leaving an empty text box for a required field If (IsNull(txtAlbum.Value)) Or...
2
by: Eddy Bee | last post by:
Hi there, I'm encountering an inexplicable problem with page formatting in reports. Here's the easiest way to explain it: The Detail section of my report contains two elements: And let's...
1
by: charliewest | last post by:
I would like to implement the following functionality: When a user taps a (child) treeNode, an event fires which allows me to get the selected treeNode's TAG object, determine it's values, and...
5
by: Verde | last post by:
I'm using a 3rd party component in an ASP.NET 1.1 Web application. The component has a .Click event that can be fired from the client, with an associated event procedure in the code-behind module....
4
by: Lars Netzel | last post by:
Is there any way to add some code that will happen in every single Event that i raised in a form? I have a Total field in the bottom of a pretty complex page and I think it's kind of "Dirty" to...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
2
by: ZBINContact | last post by:
I am creating a self-checking set of usercontrols. They tend to call their self-checking functionally in the "Load" event. I have run into a problem with my TextBox usercontrol, however, as the...
3
by: dvanderwel | last post by:
I have a .NET 1.1 web page with a DataGrid containing a TemplateColumn that contains a DropDownList. I need to fire an event when the selected item in the DropDownList changes for the current...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.