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

Home Posts Topics Members FAQ

onkeydown event on document element

I'm trying the following:

function grid() {
this._el = document.create Element("TABLE" );

var self = this;
document.addEve ntListener("onk eydown", function(event)
{self.gridKeyDo wn(event);}, false);
}

grid.prototype. gridKeyDown = function(event) {
//some code
}

var datagrid = new grid();
the addEventListene r doesn't get added, the gridKeyDown method never
gets executed. Can I add onkeydown to the document object? Is there a
good reference about mozilla dom?

Jul 23 '05 #1
10 9152
b.***@gmx.net wrote:
I'm trying the following:

function grid() {
this._el = document.create Element("TABLE" );

var self = this;
document.addEve ntListener("onk eydown", function(event)
{self.gridKeyDo wn(event);}, false);
}

grid.prototype. gridKeyDown = function(event) {
//some code
}

var datagrid = new grid();
the addEventListene r doesn't get added, the gridKeyDown method never
gets executed. Can I add onkeydown to the document object? Is there a
good reference about mozilla dom?
Other discussions are currently going regarding event handling and how
to dynamically cause it.

The simplest way to do it is also the most cross-compatible:

document.onkeyd own = function ( event ) {
event = event || window.event;
// figure out which of your grid objects it applies to
// then fire your gridKeyDown function thereon
myGrid.gridKeyD own( event );
}
// the constructor
function grid( ) {
this._el = document.create Element("TABLE" );
return this;
}

// note that you need to define the prototype before you may
// change it
grid.prototype = {
gridKeyDown: function (event) {
//some code
}
}

var datagrid = new grid();

---------

You do not need to attach the onKeyDown handler every time you create a
grid because the handler will be handling keyDown events for the entire
document, not for each individual grid object.
var self = this;
document.addEve ntListener("onk eydown", function(event)
{self.gridKeyDo wn(event);}, false);


If you do it this way, self will not be defined by the time onkeydown
handler is called, because onkeydown will be called outside of the
scope of the grid object constructor -- which is the desired behavior.

Jul 23 '05 #2
VK
addEventListene r("k*eydown", ...

All event names in addEventListene r are w/o "on", but obj.onkeydown
(with "on").
Obviously some English grammar lover (and end-developers hatter) is
working in the Mozilla team :-)

"self" is a window property reffering to the current window.
So
var self = this;
equal to
var windows.self = windows.self;
which is wrong and pointless.

Make these changes and come back ;-)

Jul 23 '05 #3
Random wrote:
If you do it this way, self will not be defined by the time onkeydown
handler is called, because onkeydown will be called outside of the
scope of the grid object constructor -- which is the desired behavior.
When he should have written: If you do it this way, self will not be defined as you expect by the
time onkeydown handlers is called, because onkeydown will be called
outside of the scope of the grid object constructor -- which is the
desired behavior. Instead, self [as VK pointed out] will be a
reference to the current window.


Jul 23 '05 #4
Thanks for the replies.

I agree that the 'var self = this' seems a little pointless, but it
works, in both IE and mozilla. Try it for yourself.
Anyway, I agree with Random that his example is the most simple.
Problem with this is that I need to keep track of all my grids in a
global array or something, ugly. I solve cross-browser issues with this
mozilla only function:

HTMLElement.pro totype.attachEv ent = function(sType, fHandler) {
sType = sType.replace(" on", "");
this.addEventLi stener(sType, fHandler, false);
}

Now I can use attachEvent on IE and mozilla. This doesn't work on the
document element however, and document.protot ype isn't allowed. My
javascript console sais: "document.proto type has no properties". Any
suggestions?

Jul 23 '05 #5
Never mind, I needed to use Document.protot ype, with a capital D

I've got it working now, tnx!

Jul 23 '05 #6
b.***@gmx.net wrote:
Thanks for the replies.

I agree that the 'var self = this' seems a little pointless, but it
works, in both IE and mozilla. Try it for yourself.
Anyway, I agree with Random that his example is the most simple.
Problem with this is that I need to keep track of all my grids in a
global array or something, ugly. I solve cross-browser issues with this
mozilla only function:

HTMLElement.pro totype.attachEv ent = function(sType, fHandler) {
sType = sType.replace(" on", "");
this.addEventLi stener(sType, fHandler, false);
}

Now I can use attachEvent on IE and mozilla. This doesn't work on the
document element however, and document.protot ype isn't allowed. My
javascript console sais: "document.proto type has no properties". Any
suggestions?


I don't believe the document object has a prototype. Did you plan on
creating new document objects inside the current window, without
replacing the current document? If not, such a prototype would be
useless anyway.

Try this, if you're intent on using attachEvent emulation:
document.attach Event = HTMLElement.pro totype.attachEv ent;

Jul 23 '05 #7
b.***@gmx.net wrote:
Never mind, I needed to use Document.protot ype, with a capital D

I've got it working now, tnx!

....

nevermind, then.

Document, huh...

ideas...

Jul 23 '05 #8
Random wrote:
Random wrote:
If you do it this way, self will not be defined by the
time onkeydown handler is called, because onkeydown will
be called outside of the scope of the grid object
constructor -- which is the desired behavior.


When he should have written:
If you do it this way, self will not be defined as you
expect by the time onkeydown handlers is called, because
onkeydown will be called outside of the scope of the grid
object constructor -- which is the desired behavior.
Instead, self [as VK pointed out] will be a reference to
the current window.


Don't pay any attention to what VK says. He has a conception of
javascript and browser scripting that differs considerably form reality.
Instead read:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >

- and find out what it means to be using a lexically scoped functional
programming language.

Richard.
Jul 23 '05 #9
b.***@gmx.net wrote:
Never mind, I needed to use Document.protot ype, with a capital D


This only works, i.e. does not result in a script error, if there is
a Document constructor, AFAIK by default restricted to the Gecko DOM.
PointedEars
--
Die erste Gunst ist Gunst, die zweite schon Verpflichtung.
-- Chinesisches Sprichwort
Jul 23 '05 #10

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

Similar topics

1
1347
by: George Hester | last post by:
This element is the only disabled=false element on the form: <HEAD> .... <SCRIPT type="text/javascript"> <!-- Begin function ValidateKey(e); e = event || window.event; var key; var skey;
1
14024
by: Rob | last post by:
I have a date text box (input type = text) in an ASP.NET/Javascript environment. I wanted to force the users to enter dates in a "__/__/____", "dd/mm/yyyy" or similar format. The textbox needs to support normal copy/paste/delete format. There wasn't much on Google to help so (after a bit of toil) though I'd post my suggested solution here. No guarantees I'm afraid; just hope it helps somebody out there. Rob Here's the ASP source code:
3
7392
by: euler | last post by:
why did the keydown event not fire in this simple example? <HTML> <HEAD><title>keydown_div</title> <script type="text/javascript"> function keydown() { alert("keydown"); } </script>
5
4369
by: Jacob Friis Larsen | last post by:
I have set window.onkeydown which works fine. One of my shortcut keys is "n", so a user can not type "n" in forms. How do I solve that? Thanks, Jacob
4
2560
by: Tony | last post by:
I'm having trouble getting Opera to recognize a repeating key event - I was wondering if anyone had experience with this. Basically, I am trying to move a <div> based on the arrow keys that are pressed - left/right motion as you press the left & right arrow keys. I want the element to move as long as the key is held down. The code I'm using is: function movePaddle(e) {
5
3786
by: Roger Withnell | last post by:
I'm using the following code to start a function on key down. document.onkeydown = OnKeyDown; function OnKeyDown() { vKeyCode = event.keyCode; ..code... }
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() {
3
2552
by: gjain12 | last post by:
Hi all, I am using the following code to disable the ctrl+a/c/v/x keys. <html> <head> <script language="JavaScript" type="text/javascript"> function disableCtrlKeyCombination(e) { //list all CTRL + key combinations you want to disable
0
8397
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
8310
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
8827
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...
1
8503
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
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
7333
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...
0
4158
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
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1620
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.