Avoiding line breaks in text areas

Asked

Viewed 1,573 times

4

I have a normal textarea

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

I wanted that when the user pressed enter, it did not break line and already send the content.

I could do that with a input? yes But the problem is that I want you to have a line break only when the user ends up typing too much (get to the end of that line).

Does anyone know if there’s a way?

  • 1

    Do a Javascript for this.

  • If you don’t want to break text, use a <input type="text">!!!

  • Naldson, the answer you marked as correct is actually incorrect. I suggest you review your choice. Think of the people who will suffer later trying to use this code that doesn’t work.

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 didn’t put as snippet because Stackoverflow blocks, but you can see it working on Jsfiddle.

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

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
    }
});

Online example on Jsfiddle.

API:

  • 1

    Or better, why not warn "Press Shift+Enter to skip line" on the page, then the form will not be sent!

  • Yours the same way when you press enter, the form is sent.

  • 1

    Dude, now when I press enter, it doesn’t break line... Blz... but it’s still not sending

  • 1

    @Nathan1302 because you don’t ask that question ^^

  • @Naldson Ué, you said you wanted the form to be sent by pressing enter. It is to send or break line? Edith your question with all the information.

  • 1

    That’s what I said @Remnan he said 2 confusing things!

  • 1

    From what I understand he wants to break the line, without having to press enter and when to press enter send the form !?!?

  • What I meant was.... When he pressed enter. Don’t break the line and send the message. In this case the line would only be broken when the user reached the end of it..... An example is this textarea that I am typing now. it does just that xD

  • I put your answer together with @P.Calixto and everything worked out fine...

  • 1

    @Naldson nice that you accepted an answer that doesn’t even work, but ok.

  • 1

    @Nathan1302 pulled out. I’m going to put an example with shift for other people who find that answer, Thanks.

  • Yes, because I think even if I press enter with the preventDefault() i believe he will keep sending the form. I think he wants when he arrives at the end of the textarea he breaks the line automatically.

Show 7 more comments

4

You can do this using javascript, just manipulate the event onkeypress of textarea, check if the key pressed is the enter and perform form Submit or anything 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
    }
  });

0

  • 2

    That code doesn’t even work because event is Undefined.

  • You can review this answer?

  • 2

    For future visitors, this answer may not work (maybe it works on IE since event is a valid object there). Here is a slightly modified version of fiddle that at least does what it was promised: https://jsfiddle.net/utluiz/sc9z66hh/7/. In the meantime I suggest looking at the other answers which are more complete.

  • Thanks for the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.