0

I have an input and an appended button. The click on button calls some function. But I don't want this function to be called when user 'presses enter key'. On the other hand, I want on keyup in this input to call some other function. SO I put

$(document).on('keyup', '#id', function(e){
        call();//calling some function
        if (e.which == 13 || event.keyCode == 13) {
            e.preventDefault();//I also tried to return false
        }
     });

But it doesn't seem to work, someone has an idea ?

4 Answers 4

1
$(document).on('keyup', '#id', function(e){
    if (event.keyCode != 13) {
        e.preventDefault();
        call();//calling some function
    }
    return false;
 });
Sign up to request clarification or add additional context in comments.

4 Comments

Still not working and I have this message : Uncaught TypeError: Cannot read property '0' of undefined
try to change the identificators
Sorry what do you mean exactly ?
@Newben the problem is not in this function.
0

Try this:

$(document).on('keyup', '#id', function(e){
    if (e.which == 13 || e.keyCode == 13) {
        e.preventDefault();//I also tried to return false
    }else{
         call();//calling some function
    }
 });

4 Comments

Still not working and I have this message : Uncaught TypeError: Cannot read property '0' of undefined
What is #id in your document? Is this unique to this elem or anyother elem also has same id.
how did you get this: Uncaught TypeError: Cannot read property '0' of undefined after inputing something
This is because the call() function returns 'invalid' when the input is not the right one
0

Don't use keyup, since the form is send on keydown.

Comments

0

Have you tried switch .call() function to a simple alert(), just for tests purpose. @Oyeme and @Jai code seems to work properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.