tags:

views:

122

answers:

2

Hey Everyone,

I am trying to get the code of an event using the event.which jQuery function. It is working great in FF, but in IE8 I am only getting "undefined". Is there some reason this would not work in IE8? I am using jQuery version 1.3.2 and the code is bellow (simplified).

function handleInput(event) {
   //Get the character code of the pressed button
   keycode = event.which;
}

Thanks, Metropolis

** EDITED **

Basically what I want to be able to do is have an HTML element with an onkeyup event attribute which will call to the handleInput function. Here is what I want the HTML to look like.

<input type="text" value="" onkeyup="handleInput(event)" />

Is there a way to pass "event" like this and have jQuery recognize it?

A: 

event.which is not jQuery specific. Have you tried using event.keyCode?

Reading Material

Jeremy
Thanks for the reply Jer. I already know how to use events normally. I would like to do this using jQuery if possible though that way that jQuery can take care of any cross platform incompatibilities for me.
Metropolis
A: 

jQuery does support event.which. How exactly are you binding your handler? Are you using jQuery? If you're doing:

jQuery("#element").click(function(evt) {
 //evt is a jQuery-normalized event object

});

It should work. You probably don't want to use event (notice that I am passing in evt versus event) as the parameter since it will collide with IE's event object. I believe this is the source of your problem.

EDIT

After reading your comment, you need to do something like this in a Javascript file that you reference from <HEAD>:

jQuery(document).ready(function() {
   //event-handling binding-code
});
Vivin Paliath
Thanks for the reply Vivin. Right now I am calling this function from an HTML element and passing "event". Is there a way to pass in an event like this and have jQuery recognize it? I will not always know which element I am attaching it to since it is dynamic.
Metropolis
What do you mean when you say that the element is dynamic?
Vivin Paliath
Well I am using this inside a library, so I will be using this function in many different places. I did not really want to be adding the jQuery("#element").click inside a javascript file on every project I use this in. I was hoping to be able to keep it inline with an HTML element like above.
Metropolis
Well either way you need to separate presentation (HTML) from behavior (Javascript). So you would need at the very least a Javascript file that you reference in `<HEAD>`. Check out my answer for an example
Vivin Paliath
I know I can do it that way.....I was just wanting to know if there is a way to do it the way I showed above?
Metropolis
I don't believe so. Even if it were, that would be a risky way. Why would you want to use `event` when you know it's ambiguous? So my suggestion is to programmatically bind events :)
Vivin Paliath
Ok, thanks Vivin for all your help. I will just assume I can not do it, and forget about trying to do it that way :)
Metropolis
I'm assuming the way you wanted to do it was with the `event` object? Yeah, that would be harder. As far as writing in the code in HTML I believe you *can* do that with jQuery, but I would advise against it. It's much better when you separate behavior from presentation. Makes it easier in the long run :)
Vivin Paliath