469,923 Members | 1,777 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Help Capturing an onkeyup event

Hi. I know the basic way to assign event handlers:

<input onKeyUp="processEvent(event)" />

But how do I assign a function to the onKeyUp event in *javascript*
that can access the event object?

// get inputElement
alert("Some java script code");
inputElement.onkeyup = ????;

I know how to assign generic functions. This works:

inputElement.onkeyup = function() { alert("hello"); };

But how in the world do I assign a function that captures the "event"
object? I want something like:

inputElement.onkeyup = function(event) { processEvent(event) };

But that doesn't work right. Any ideas?
Jul 23 '05 #1
3 8677


Trent wrote:

But how do I assign a function to the onKeyUp event in *javascript*
that can access the event object?

// get inputElement
alert("Some java script code");
inputElement.onkeyup = ????;

I know how to assign generic functions. This works:

inputElement.onkeyup = function() { alert("hello"); };

But how in the world do I assign a function that captures the "event"
object? I want something like:

inputElement.onkeyup = function(event) { processEvent(event) };

But that doesn't work right. Any ideas?


That last attempt should work with Mozilla/Netscape however the problem
is that IE has a global variable window.event that your event parameter
shadows so you need
inputElement.onkeyup = function (evt) {
if (!evt) {
evt = window.event;
}
// now use evt e.g.
alert(evt.keyCode);
};
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
Trent wrote:
Hi. I know the basic way to assign event handlers:

<input onKeyUp="processEvent(event)" />


No. You must decide if you want to use HTML

<!-- attribute name case does not matter, no trailing / -->
<iNpUt onKeyUp="processEvent(event)">

or XHTML

<!-- element and attribute names must be lowercased,
trailing / (empty content model) -->
<input onkeyup="processEvent(event)"/>
PointedEars
Jul 23 '05 #3
Does it work well finally? Today I need to implement the onkeyup event
but I found it doesn't work consistently. usually, the first few stroke
does not trigger the event, till I switch to another field and back to
this field. Then, the event can be fired for every keystroke. Any
idea?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Evan Wong | last post: by
3 posts views Thread by Brad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.