473,883 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.create Event("Event");
f.initEvent(e.t ype, true, true);
for(p in e) {
if(typeof f[p] === "undefined" ) {
f[p] = e[p];
}
}
elem.dispatchEv ent(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 7530
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.create Event("Event");
f.initEvent(e.t ype, true, true);
for(p in e) {
if(typeof f[p] === "undefined" ) {
f[p] = e[p];
}
}
elem.dispatchEv ent(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.mozil la.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.javas cript 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
10197
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 (Len(txtAlbum.Value) < 1) Then MsgBox("Album Title cannot be empty") txtAlbum.SetFocus End If
2
3909
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 say I have the following VB code for the Detail_Format event:
1
1782
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 then load a new form based on those values... Intellisense shows the "Click" event, however, this does nothing. (I assume it's not supported in .Net CF?) I see via Design Mode in VS 2003 that the only feasible event is "AfterSelected". This,...
5
2045
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. The logic in the click event procedure may, under some conditions, need to abort further processing. AFAIK the control's event is not cancellable. At least its EventArgs does not have any apparent support for .Cancel. So, what can I do to...
4
1283
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 update that Filed from every single Event that might affect it Is there are was to keep that Filed Total (a value) updated at all time without having to think about it in every single event on the page?
3
3999
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 about the control they just entered (sort of like an extended tooltip). Now, I can certainly create a separate mouseenter event for each control (too much work, and not very clever), but what I would like to do is somehow create one event that would...
5
6515
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); td.onclick = myfunction; function myfunction(event) { alert(event);
2
2731
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 base component does NOT have a Load event as far as i can ascertain. What I need is to have a self-check function called when the component loads. I am currently using the "Layout" event but that can fire multiple times, and I can't just call...
3
3220
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 DataGrid row, and I'm having trouble figuring out how to do this. Other control types I have defined the "CommandName" property in the aspx file. The event returns the index of the row that contains the control, and I can thus pull more data out...
0
9944
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10860
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3239
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.