Jump to content

Catch Onkeydown Event


Ryan Mann
 Share

Recommended Posts

Input.onkeydown = function(){		if(window.event)	{		var Key = window.event.keyCode;	};	if(event.which)	{		var Key = event.which;	};	if(Key == 13)	{		alert('enter');	};};

The problem lies with the event.which bit. "event is not defined". I know how to do it regularly, but I am trying to do it unobtrusively. Thank you.

Link to comment
Share on other sites

I think you trying to achieve something like this:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Untitled Document</title></head><body><script type="text/javascript">function keypressed(e){e = e? e : window.event;var Key = e.keyCode? e.keyCode : e.which? e.which : null;if (Key == 13){alert("Enter button Pressed - Press the damn 'Submit Now' button Fool!")if(e.preventDefault)e.preventDefault(); //Prevent form being submittinge.returnValue=false; //Prevent form being submittinge.cancel = true; //Prevent form being submitting};};</script><form><input type="text" onkeypress="keypressed(event)" /><input type="submit" value=" Submit Now" /></form></body></html>

Edited by dsonesuk
Link to comment
Share on other sites

Yes, but unobtrusive. My code works fine in IE, it's just capturing the event in Firefox.
"window.event" is exclusive to Internet Explorer, and is not standard either. The correct way is to capture the event object as the parameter of the function that is called with the event.
Link to comment
Share on other sites

or maybe:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/window.onload = function() { //Wait for the page to load. var inputs = document.getElementsByTagName('input'); for(var i=0;i<inputs.length;i++) { if(inputs.getAttribute('type')!="submit") { if(window.event) { inputs.onkeypress = function() { keypressed(event)}; } else { inputs.setAttribute('onkeypress', 'keypressed(event)'); } } } }function keypressed(e){ e = e? e : window.event;var Key = e.keyCode? e.keyCode : e.which? e.which : null;if (Key == 13){alert("Enter button Pressed - Press the damn 'Submit Now' button Fool!")if(e.preventDefault)e.preventDefault(); //Prevent form being submitting e.returnValue=false; //Prevent form being submittinge.cancel = true; //Prevent form being submitting};}/*--*//*]]>*/</script> </head><body><form><input type="text" /><input type="text" /><input type="text" /><input type="submit" value=" Submit Now" /></form></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...