epsilones
epsilones

Reputation: 11609

How to prevent 'pressing enter' in an input to trigger an event ? Java script

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 ?

Upvotes: 0

Views: 170

Answers (4)

Felipe Miosso
Felipe Miosso

Reputation: 7339

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

Upvotes: 0

Oyeme
Oyeme

Reputation: 11225

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

Upvotes: 1

Byscripts
Byscripts

Reputation: 2588

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

Upvotes: 0

Jai
Jai

Reputation: 74738

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
    }
 });

Upvotes: 0

Related Questions