469,295 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

How to fire a JS event when "Enter" key is pressed ?

296 100+
Hi,

I want to invoke a JS function when "Enter" key is pressed ?

Using the onkeydown event will be a way but how to track the "Enter" key ?
Feb 12 '07 #1
11 32128
dmjpro
2,476 2GB
u better to use onkeypress on onkeyup.....
onkeydown will work also....
function checkEnter()
{
if(event)
{
if(evnet.keyCode == 13) //ur code
}
else if(evt)
{
if(evt.keyCode == 13) //ur code
}
}
Feb 12 '07 #2
acoder
16,027 Expert Mod 8TB
Hi,

I want to invoke a JS function when "Enter" key is pressed ?

Using the onkeydown event will be a way but how to track the "Enter" key ?
See this page. The code for the enter key is 13 (as dmjpro pointed out).
Feb 12 '07 #3
acoder
16,027 Expert Mod 8TB
u better to use onkeypress on onkeyup.....
onkeydown will work also....
function checkEnter()
{
if(event)
{
if(evnet.keyCode == 13) //ur code
}
else if(evt)
{
if(evt.keyCode == 13) //ur code
}
}
onkeyup is fine. Also, for Firefox and other non-IE browsers, you should use the event argument for the function,e.g.
Expand|Select|Wrap|Line Numbers
  1. function checkEnter(event)
  2. {
  3.    if (event.keyCode == 13) //ur code
  4. }
Feb 12 '07 #4
pankajit09
296 100+
What is the problem with onkeydown ?
Feb 13 '07 #5
pankajit09
296 100+
Also please tell me whether the following code is correct -->

Expand|Select|Wrap|Line Numbers
  1. function checkEnter(e){ 
  2.         var characterCode
  3.         if(e && e.which){ 
  4.             e = e
  5.             characterCode = e.which 
  6.         } else {
  7.             e = event
  8.             characterCode = e.keyCode
  9.         }
  10.         if(characterCode == 13){ 
  11.             getSearchResults(); 
  12.         }
  13.     }
  14.  
Feb 13 '07 #6
dmjpro
2,476 2GB
ur code is fine -- for cross browser check

kewdown is .... when a key just gets down then the event fires ...
if the user continues press the enter key without releasing then the event will fire many times with in a second....
that's why it is not suitable ...

if u want only once the event will fire even user continues press it ..
then try this code ..

function fun_name(e)
{
if(charactercode == 13)
{
//disable that control so that it is no more to take enter key
//ur code
}
}

try do one more thing while u writing js code ...
it is not mandatory to put ; after each line ...
but it is a bad practice ...
good practice to put ; after each line

welcome in advance
Feb 13 '07 #7
pankajit09
296 100+
Thanks a lot !!
Feb 13 '07 #8
pankajit09
296 100+
Is e=e required in the above code ?
Feb 13 '07 #9
acoder
16,027 Expert Mod 8TB
Use this code in your function:
Expand|Select|Wrap|Line Numbers
  1. var code;
  2. if (!e) var e = window.event;
  3. if (e.keyCode) code = e.keyCode;
  4. else if (e.which) code = e.which;
Then code will contain the key pressed.
Feb 13 '07 #10
Use this code:-

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function keypress(e)
  3. {
  4.     var Ucode=e.keyCode? e.keyCode : e.charCode
  5.     if (Ucode == 13)
  6.     {
  7.         //write the code for submit
  8.         AddChat(page);
  9.     }
  10. }
  11. </script> 
  12.  
***************************************
add this event on text box onkeypress="keypress(event);"

Regards
Vindesh Mohariya(Lowanshi)
Feb 5 '08 #11
acoder
16,027 Expert Mod 8TB
Don't post your email address and please use code tags when posting code. Thanks!

Moderator.
Feb 5 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Byron McClain | last post: by
2 posts views Thread by Cindy | last post: by
11 posts views Thread by Sven | last post: by
1 post views Thread by Gouri.Mahajan7 | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.