1

Look at my code, if you used chrome, you will notice that the preventDefault doesn't work when I press the Enter Key, but works in firefox and safari, why?

var fighting_textarea=$('#fighting');
   
   fighting_textarea.on( "keydown", function( event ) {
    if ( event.which == 13 ) {
        event.preventDefault();
     alert('hello');
    }
    
   });//click
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="fighting"> </textarea>
conan
  • 1,327
  • 1
  • 12
  • 27

2 Answers2

1

It is working fine, you just have to remove alert(i guess it is not required). One more thing you should also consider code 10 too, b'coz on iphone safari gives code 10.

var fighting_textarea=$('#fighting');
   
   fighting_textarea.on( "keydown", function( event ) {
    if ( event.keyCode == 10 || event.keyCode == 13 ) {
        event.preventDefault();
     
    }
    
   });//click
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="fighting"> </textarea>
1

You could use keypress instead of keydown

var fighting_textarea = $('#fighting');
fighting_textarea.on("keypress", function (event) {
    if (event.which == 13) {
        event.preventDefault();
        alert('hello');
    }

}); //click

Also take a look at this answer

Also just noticed that if you remove the alert everything seems to be ok, as to why unfortunetly i have no idea.

Community
  • 1
  • 1
Bobby Tables
  • 2,953
  • 7
  • 29
  • 53