Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable linebreaks in HTML textarea

with CSS or any other way, is it possible to disallow newlines in a HTML textarea? So basically I want nothing to happen when the user presses "enter".

like image 228
Franz Avatar asked Jan 10 '11 21:01

Franz


People also ask

How do I preserve line breaks when getting text from a textarea?

To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n' . const post = document. createElement("p"); post. textContent = postText; post.

How do you keep a line break in HTML?

The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

How do you break a line in CSS?

A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).


1 Answers

Using jQuery, you could do it like this:

$(document).ready(function() {
  $("#t_area").keypress(function(event) {
    if(event.which == '13') {
      return false;
    }
  });
});

<textarea id="t_area"></textarea>

I would also strip out any newlines using PHP or ASP or whatever server-side scripting language you have access to as well to be completely thorough.

like image 174
Michael Irigoyen Avatar answered Sep 30 '22 17:09

Michael Irigoyen