javascript - preventDefault() not working in a input type text -


this code:

$('#login').keydown(function(e){                  if(e.which  == 32){                  alert("enter");             this.preventdefault();                     }             }); 

this code doesn't work sends alert

$('#pwd,#pwd2').on('keydown',function(e){         if(e.which == 32){             e.preventdefault();         }     }); 

and code works. not understand why.

you wanting prevent default action of event (which passed in parameter event function, calling e).

this element target of event (ie: in case e.target #login element) , not event itself, not have preventdefault().

just use event object well:

$('#login').keydown(function(e){              if(e.which == 32){              e.preventdefault();                 }         }); 

with above you'll see event cancelled every time hit space bar within #login: http://jsfiddle.net/xwmuu/


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -