Jump to content
Sign in to follow this  
Ryan Mann

Catch Onkeydown Event

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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
alert("Enter button Pressed - Press the damn 'Submit Now' button Fool!")
Sweet error message.

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...