cpd Posted July 27, 2009 Share Posted July 27, 2009 Im having some trouble with an "On Enter" function. Its extremely baisic and should work fine but it doesnt. The problem Whenever you hit a key, it doesnt seem to register for some reason. But when you hit the enter key it submits every time without hesitation. Question Can anyone give me a function in JavaScript thats guarenteed to work and be quick and not hesitate or stop people from typing which is whats currently happening. My Code // This is in the input tag onkeyup="keyupL(event.keyCode);" //This is the javascript function keyupL(arg1) { if (arg1 == 13) postMessage(); } I have tried onkeypress and onkeydown but there is no difference. Cheers Chris Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2009 Share Posted July 27, 2009 Your request isn't clear. It appears you are trying to implement JavaScript functionality to automatically submit a form when the user presses enter. A properly constructed form will do that automatically as long as you have a submit button or elementon the page. I am thinking that the enter key is working right away because of the default form behavior and not your script. You probably need to put an onsubmit() trigger on your form if you don't want it submitted automatically on the enter key. Honestly, I have no clue what you are trying to achieve. So, I can't lend any advice. Quote Link to comment Share on other sites More sharing options...
cpd Posted July 27, 2009 Author Share Posted July 27, 2009 Yeah i tried to be as clear as possible. My input tag isnt wrapped ina form at all!! Its simply an input tag thats a text type and onKeyDown i see if the user is pressing the enter key, if so then post the message. Currently it stops some keys from being tapped and you have to hit the key several times before it types the letter. Do you understand now? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2009 Share Posted July 27, 2009 Ok. I had tried the code you posted and it worked for me with no problems. Don't know what keys you are having problems with, but I had not problems. By the way, the code you have would only work in IE as other browsers don't recognize the "event" object. Quote Link to comment Share on other sites More sharing options...
cpd Posted July 27, 2009 Author Share Posted July 27, 2009 aww right wicked, im using firefox. Is there any chance you could help me write code for majority of web browsers if not all Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2009 Share Posted July 27, 2009 <html> <head> <script language="JavaScript" type="text/javascript"> function enterCheck(e) { var keyCode = (e.keyCode) ? e.keyCode : e.charCode ? e.charCode : e.which; if (keyCode == 13) { alert('User pressed enter key'); } } </script> </head> <body> <input type="text" name="test_field" onkeypress="enterCheck(event);" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
cpd Posted July 28, 2009 Author Share Posted July 28, 2009 That works fine. I tested it in firefox and then in opera (which i hadnt ever done before). In firefox i still have the same problem whereby it doesnt register the keys all the time. I may have to hit a key several times to get it to type whereas in opera it is absolutely fine. Any ideas? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 28, 2009 Share Posted July 28, 2009 Worked fine for me in FF. I suspect there may be some other code on the page that is interfering, but that's just a guess Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.