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
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
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
Share on other sites

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.

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.