Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea enter keypress not working because of form submit enter prevention

I have a form in which I've used the following code to prevent the form being submitted on the press of 'Enter'.

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

</script>

As a result, the 'Enter' key is not working in any textarea input. I can't enter a new line because of the body function. How do I solve this?

<textarea name='description' placeholder="Any other information (optional)"</textarea>
like image 612
Harsh Gupta Avatar asked Jan 09 '23 11:01

Harsh Gupta


2 Answers

I have find solution.

You prevent enter key on all the form element. Just add some tweak to your code and its done. Just skip prevention of enter key when your focus is on textarea. See below code :

$(document).ready(function() {
  $(window).keydown(function(event){
      if(event.target.tagName != 'TEXTAREA') {
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      }
  });
});
like image 137
Rav's Patel Avatar answered Jan 11 '23 02:01

Rav's Patel


To prevent the form from submitting, try this instead:

$("form").submit(function(e){
  e.preventDefault();
}
like image 44
alan0xd7 Avatar answered Jan 11 '23 01:01

alan0xd7