1

I have a textarea where it should not be possible to input line breaks by pressing enter. How can I disable enter in my textarea?

That is the code:

<textarea name="textarea" style="width:250px;height:150px;"></textarea>
mingos
  • 21,858
  • 11
  • 68
  • 105
Bam Sam
  • 13
  • 1
  • 5
  • 4
    Why would you want to do that? If the user is not allowed to add new lines, then just use an ``. – str Dec 28 '16 at 10:52
  • 2
    Possible duplicate of [disable enter key on specific textboxes](http://stackoverflow.com/questions/5482825/disable-enter-key-on-specific-textboxes) – Luzan Baral Dec 28 '16 at 10:54
  • 1
    Possible duplicate of [Disable New Line in Textarea when Pressed ENTER](http://stackoverflow.com/questions/18779322/disable-new-line-in-textarea-when-pressed-enter) –  Dec 28 '16 at 10:57
  • @str can I also enlarge the input? – Bam Sam Dec 28 '16 at 10:59
  • @BamSam Yes, you can. – str Dec 28 '16 at 11:18

3 Answers3

7

Don't forget to use the keydown event instead of the click event

   $("input").keydown(function(event) {
    if (event.keyCode == 13) {
        event.preventDefault();
    }
});
Fola Azeez
  • 91
  • 1
  • 4
0

If you use jquery in your web application you can do fallowing trick to disable enter key.

$('textarea').keydown(function(e) {
    if(e.which == 13) { return false; }
});

else you can use this

document.getElementById('textarea_id').addEventListener('keydown', function(k){
    if(k.keyCode == 13) return false;
});

I hope this will help you!

ustmaestro
  • 998
  • 10
  • 19
-2

Use event.preventdefault and next do what you like. For example

<script>
$("textarea").click(function(event) {
  if (event.keyCode == 13) {
        event.preventDefault();
    }
});
</script>
tam nguyen
  • 156
  • 7