Page 1 of 1

jQuery keypress in element?

Posted: Tue Dec 18, 2007 10:19 pm
by alex.barylski
Is it possible to determine which element an keypress event fired in using jQuery?

I've trapped the keypress/keydown, etc...and it appears the only property exposed to clients is the

Code: Select all

e.which == 32 // SPACE BAR PRESSSED
I was amazed to find that something like:

Code: Select all

e.element
Wasn't provided...that I could tell anyways???

Anyone have any experience or insight as to how I could add this functionality to jQuery or determine the element which triggered the event?

I have hacked on a solution for IE only...but that kind of defeats the purpose of using jQuery :P

Posted: Tue Dec 18, 2007 10:41 pm
by Kieran Huggins
This may come in handy, or make you want to jump out a window:
http://www.quirksmode.org/js/keys.html

Posted: Tue Dec 18, 2007 11:23 pm
by alex.barylski
I was really hoping to avoid using anything but jQuery to capture events, but I guess I have no choice...

Posted: Wed Dec 19, 2007 11:00 am
by pickle
Maybe something like this (never tried, but imagine it would work):

Code: Select all

$(document).ready(function(){
   $(document).click(function(event)){
      //event.target now is the element that was clicked
   });
});
Note: The entire line below is the link but phpBB is treating the () weird
http://docs.jquery.com/Events_(Guide)

Posted: Wed Dec 19, 2007 11:12 am
by Kieran Huggins
I just now understood what you were asking - sorry!

'this' should refer to the object that triggers the event (the context object)

Code: Select all

$(function(){
	$('#test').keyup(function(event){
		alert(this.id); // "test"
		alert(event.type); // "keyup"
	})
})

Posted: Wed Dec 19, 2007 2:28 pm
by alex.barylski
Thanks for the foodback guys...I'll try your suggestions shortly hopefully. :)