La3eb
La3eb

Reputation: 27

how to make textarea not start a new line?

how to make to make textareas not start a new line each time I click on enter I need to do this with javascript not Jquery

event.prevent Default();

so I need an alternative
Thankyou

Upvotes: 0

Views: 106

Answers (3)

Ideal Bakija
Ideal Bakija

Reputation: 639

Well you want to prevent the default action of 'enter' on the textarea so :

HTML code:

<textarea id="textarea" onkeyup="function()"></textarea>

Javascript code:

document.getElementById('textarea').onkeypress = function(e) { 
        if(e.which== 13){
           e.preventDefault();
        }
};

jQuery:

$('textarea').keyup(function(e){

    if(e.Which == 13){
      e.preventDefault();
    }

});

The "e.which == 13" checks if enter is pressed, and prevents the default action (which was previously a carriage return).

Upvotes: 0

kruczy
kruczy

Reputation: 791

use css resize

textarea {
    resize: none;
}

Upvotes: 0

Hornth
Hornth

Reputation: 323

You can try this :

 function preventEnter(evt) {
     if(evt.Which == 13){
         evt.preventDefault();
     }
 }

 document.getElementById('my-textarea').addEventListener(
     'keypress', preventEnter, false
 );

Upvotes: 1

Related Questions