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".
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With