473,657 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with onkeydown in div (IE 6.0)

why did the keydown event not fire in this simple example?
<HTML>
<HEAD><title>ke ydown_div</title>
<script type="text/javascript">
function keydown() {
alert("keydown" );
}
</script>
</HEAD>
<BODY>

<table border="1" align="center" >
<tr >
<td><div id="a" onkeydown="keyd own()">a</div></td>
<td><div id="b">b</div></td>
</tr><tr>
<td><div id="c">c</div></td>
<td><div id="d">d</div></td>
</tr>
</table>
</BODY>
</HTML>
Jul 23 '05 #1
3 7391


euler wrote:
why did the keydown event not fire in this simple example?
<script type="text/javascript">
function keydown() {
alert("keydown" );
}
</script>
</HEAD>
<BODY>

<table border="1" align="center" >
<tr >
<td><div id="a" onkeydown="keyd own()">a</div></td>


With IE keydown doesn't fire for all keys so unless you tell us which
keys you tried we would need to guess.
And somehow the div would need focus I think to be able to receive key
events.
What happens when you click in the div content and then hit keys, do you
get the key event handler fired then?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen <ma*******@yaho o.de> wrote in message news:<42******* *************** *@newsread2.arc or-online.net>...
euler wrote:
why did the keydown event not fire in this simple example?


<script type="text/javascript">
function keydown() {
alert("keydown" );
}
</script>
</HEAD>
<BODY>

<table border="1" align="center" >
<tr >
<td><div id="a" onkeydown="keyd own()">a</div></td>


With IE keydown doesn't fire for all keys so unless you tell us which
keys you tried we would need to guess.
And somehow the div would need focus I think to be able to receive key
events.
What happens when you click in the div content and then hit keys, do you
get the key event handler fired then?

hello Martin,

thank you for answering, but I didn't get it yet.

When using onclick instead of onkeydown, the keyhandler is invoked, I
know.
In the documentation there is said that onkeydown applies to the
div-Element,
nothing is said about further conditions like element must have focus.

Also after setting the focus to the div-Element(hope it's right) the
pressing of e.g. Enter-key doesn't show me the alert of the keydown
handler.

where is the mistake? or is this a bug?

<HTML>
<HEAD><title>ke ydown_div</title>
<script type="text/javascript">
function keydown() {
alert("keydown" );
}
function onload() {
document.getEle mentById('a').f ocus();
}
</script>
</HEAD>
<body onload="onload( )">

<table border="1" align="center" >
<tr >
<td><div id="a" onkeydown="keyd own()">a</div></td>
<td><div id="b">b</div></td>
</tr><tr>
<td><div id="c">c</div></td>
<td><div id="d" onclick="keydow n()">d</div></td>
</tr>
</table>
</BODY>
</HTML>
Jul 23 '05 #3


euler wrote:

In the documentation there is said that onkeydown applies to the
div-Element,
nothing is said about further conditions like element must have focus.


But a normal element like a div doesn't take keyboard input, you can use
an onkeydown/press/up event handler on a div or p but of course it will
only fire if the element contains controls taking keyboard input, the
user types in the control and then the key event bubbles up to the
container div or p.

Here is an example using a normal paragraph without controls, a
paragraph with an input control, and an editable paragraph (IE 5.5/6
only), you will see that the key event handlers on the second paragraph
fire when text is entered in the input control:

<html lang="en">
<head>
<title>key event handlers on normal element not being controls</title>
<script type="text/javascript">
function reportEvent (evt, element) {
var result = evt.type + ': ';
result += 'handled at: ' + element.tagName + '; ';
result += 'target/srcElement: ' + (evt.target ? evt.target :
evt.srcElement. tagName) + '; ';
document.body.a ppendChild(docu ment.createText Node(result));
document.body.a ppendChild(docu ment.createElem ent('br'));
}
</script>
</head>
<body>

<h1>key event handlers on normal element not being controls</h1>

<p onkeydown="repo rtEvent(event, this);"
onkeypress="rep ortEvent(event, this);"
onkeyup="report Event(event, this);">
Paragraph with no controls.
</p>

<p onkeydown="repo rtEvent(event, this);"
onkeypress="rep ortEvent(event, this);"
onkeyup="report Event(event, this);">
Paragraph with input control:
<input type="text">.
</p>

<p onkeydown="repo rtEvent(event, this);"
onkeypress="rep ortEvent(event, this);"
onkeyup="report Event(event, this);"
contentEditable ="true">
Editable paragraph.
</p>

</body>
</html>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3903
by: Z | last post by:
I have sub-classed the TextBox. In its OnKeyDown event I can intercept key strokes, examine them, etc. When I get a certain keycode (e.g., 'A') I want to change it to another unicode key from a different code page (e.g., "\u2801"). But the KeyCode, KeyData, and KeyValue properties of KeyEventArgs are all only getters. They are read only and thus do not permit changing the assigned KeyCode. My question is this: How do I change the KeyCode...
4
9999
by: KublaiKhan | last post by:
I am trying to figure out what values are returned when keys are pressed in Mozilla & IE. Here's the code I'm using. There are no alerts in either browser. Any clues would be greatly appreciated. Thanks. KK <html> <head><title>Reading Keystrokes</title> <script language="JavaScript"> window.onkeyDown = keyHit;
17
50280
by: Julia Briggs | last post by:
Are there any gotchas using if (event.keyCode==8)? I understand that to represent backspace, but it doesn't work. I am running Windows XP, using a typical keyboard - but no luck in detecting backspaces. Anyone have experiences with this?
10
9152
by: b.dam | last post by:
I'm trying the following: function grid() { this._el = document.createElement("TABLE"); var self = this; document.addEventListener("onkeydown", function(event) {self.gridKeyDown(event);}, false); }
0
1139
by: =?Utf-8?B?Q0dX?= | last post by:
I have a .NET 1.1 application which uses several grids of text boxs (in repeaters and datagrids) for entering arrays of time values. I use onkeydown to allow users to navigate through the grids using arrow keys. I'm attempting to upgrade to .NET 2.0, but that particular functionality appears to be broken now. The actual javascript function is quite long and really irrelevant since the problem appears to be with the onkeydown event firing...
0
1184
by: CGW | last post by:
I posted this in a .NET general newsgroup, but then found this group and I think it would be more appropriately posted here. Even though the problem is partially with client side behavior, I'm attempting to deal with it in my VB code behind. Here's my post... I have a .NET 1.1 application which uses several grids of text boxs (in repeaters and datagrids) for entering arrays of time values. I use onkeydown to allow users to navigate...
1
4940
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: http://munich.schwarz-interactive.de/autocomplete.aspx
1
5744
eboyjr14
by: eboyjr14 | last post by:
I have this UserScript for Grease monkey. but I can't get the onleydown event to fire in FIREFOX only. I've looked everywhere! // ==UserScript== // @name iGoogle Suggest // @namespace Devin Samarin // @description This automatcally selects the top result for your query. // @include *google.com/ig* // ==/UserScript== function makeRequest() {
8
4261
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these raise an event how do I create an handler to catch these raised events. //Tony
0
8826
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
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4155
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...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
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.