Avoiding line breaks in text areas [duplicate]

4

I have a normal textarea

<textarea placeholder=" Digite aqui" class="form-control"></textarea>

I wanted the user to press enter when he did not break the line, but already sent the content.

Could you do this with a input ? yea But the problem is that I want it to have a line break only when the user finishes typing more (get to the end of that line).

Does anyone know if it has?

    
asked by anonymous 21.12.2015 / 19:20

3 answers

5

As you are using Bootstrap, the example below uses jQuery.

var ENTER_KEY = 13;

$('textarea').on('keypress', function(event) {
  var char = event.which || event.keyCode;
  if (char == ENTER_KEY) {
    event.preventDefault();
    $(this).parent('form').submit();
  }
})

I did not put snippet because StackOverflow blocks, but you can see it working in JSFiddle strong> .

If you want to keep the line break when the user presses SHIFT + ENTER you can check with mouveEvent#shiftKey() that returns true if shift is pressed: p>

var ENTER_KEY = 13;

$('textarea').on('keypress', function(event){
    var char = event.which || event.keyCode;

    if(char == ENTER_KEY){
       event.preventDefault();
       if(event.shiftKey){
           $(this).val($(this).val() + '\n'); // shift + enter = Quebra linha
           return;
       }
       $(this).parent('form').submit(); // somente o Enter = Envia o form
    }
});

Example online at JSFiddle .

API:

21.12.2015 / 19:40
4

You can do this using javascript, just manipulate the onkeypress event of the textarea , check if the pressed key is enter and perform the form submit or whatever else you wish.

document.getElementById('meuTextarea')
  .addEventListener('keypress', function(e) {
    var key = e.which || e.keyCode;
    if (key === 13) {
      e.preventDefault(); //Evitar a quebra de linha (remove o comportamento padrão)
      document.getElementById("myForm").submit(); //Submeter o formulário
    }
  });
    
21.12.2015 / 19:45
0

Here's another option in jquery link

$(document).ready(function(){
  $( "#teste" ).keypress(function() {
   if(event.keyCode == 13){
     $( "#myform" ).submit();
   }
  });
});
    
21.12.2015 / 19:55