473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 32503
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

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

Similar topics

1
by: Byron McClain | last post by:
.... is being consumed. I added an event handler for the "keypress" event and my delegate never gets executed. Why? I am trying to catch the "enter" key pressed event to prevent the DataGrid...
2
by: Cindy | last post by:
Hi all you smarties out there, I'm having a little conundrum with my asp.net page Scenario: I have a form (asp.net) with no code behind (as yet). I have placed a javascript function on a...
11
by: Sven | last post by:
I want this: when I press commandbutton1, then the program should send the keystroke "enter" to text1.text... how can I do this?
3
by: Al | last post by:
Hi, I have simple question does any one knows hoe to make a button receive click even when enter is pressed. Thanks Al
2
by: RTT | last post by:
<form id="Form1" method="post" runat="server"> <asp:textbox id="TxtEmail" tabIndex="1" runat="server" Width="272px" ToolTip="Enter your emailaddress for authentication"></asp:textbox> <asp:textbox...
2
by: kohoutec | last post by:
Hi Im very much a beginner so apologies if my terminology is wrong. I have a combo box with some items already loaded, the users can also enter a new item. If they enter a new item and then...
2
by: matthewr | last post by:
In Internet Explorer, for example, when you hit return in the address bar, the Go button is pressed. In my program, I have a toolstrip with a textbox and button. How do I ensure the button is...
1
by: Cub71 | last post by:
How can I avoid a "ding" when enter is pressed in a textbox with multiline == false?
1
by: Gouri.Mahajan7 | last post by:
Hello, I am loading the user control on the web page. In the user control i have added the key down event, when enter key is pressed i am displaying some message. When I run the user control...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.