Jump to content

On Enter


cpd

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/167628-on-enter/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167628-on-enter/#findComment-884081
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/167628-on-enter/#findComment-884186
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/167628-on-enter/#findComment-884359
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/167628-on-enter/#findComment-884395
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.