Here are a couple options:

1) Set up the keypress binding on the element(s) and then wrap your switch
in a conditional (if) that checks a flag your test link click would set to
true

2) Event delegation: Set up the keypress binding at the document/some other
higher level in the DOM. Use event.target to check whether the key was
pressed on an element on which you want to handle 'a' or 's', otherwise
ignore.

- Richard

Richard D. Worth
http://rdworth.org/

On Thu, Aug 28, 2008 at 12:35 AM, hubbs <[EMAIL PROTECTED]> wrote:

>
> I have a simple keypress event that throws an alert when you press "a"
> or "s".  Is it possible to bind this event to specific elements, so
> that it is not a global keypress, but only fired if the test link is
> first clicked.  Then, $(this) would be used to get attributes of the
> clicked element if needed.
>
> Any ideas on how to get that to work, or if it is possible?
>
> $("#test").keypress(function(e)
>        {
>                switch(e.which)
>                {
>                        case 97:        alert("A pushed!");
>                                                break;
>
>                        // user presses the "s" key
>                        case 115:       alert("S pushed!")
>                }
>        });
>

Reply via email to