Textarea type keypress not working due to form submit enter prevent

I have a form where I used the following code to prevent the form from being submitted when 'Enter' is pressed.

<script>
$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});

</script>

      

As a result, the 'Enter' key does not work in any text field. I cannot enter a new line due to the body function. How to solve this?

<textarea name='description' placeholder="Any other information (optional)"</textarea>

      

+3


source to share


2 answers


I have a solution.

You are preventing the key from being entered in the entire form element. Just add some customization to your code and execute it. Just skip the enter key warning when your focus is on the textarea. See below code:



$(document).ready(function() {
  $(window).keydown(function(event){
      if(event.target.tagName != 'TEXTAREA') {
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      }
  });
});

      

+6


source


To prevent the form from submitting try this instead:



$("form").submit(function(e){
  e.preventDefault();
}

      

+1


source







All Articles