Paramore
Paramore

Reputation: 1313

JQuery - focus triggers keyup event in target input

I will demonstrate my problem using the simple example. Consider the following script

   $(document).on('keydown','#input1', function(e) 
   {        
      if(e.keyCode==13){ $("#input2").focus(); }    
   });

and HTML

   <input type="text" id="input1"/>
   <input type="text" id="input2" onkeyup='alert("UP")'/>

every time I press enter in the first input, focus goes to the second input but keyup event is triggered also. I tried stopPropagation but it does not work. How can I prevent that issue?

Upvotes: 4

Views: 3364

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

I'd say you could use keyup instead of keydown

$(document).on('keyup','#input1', function(e) 
{        
    if(e.keyCode==13){ $("#input2").focus(); }    
});

jsFiddle Demo

Upvotes: 2

Related Questions