Reputation: 27
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
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
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