How to add line breaks to an HTML textarea with JavaScript?

Spread the love

Sometimes, we want to add line breaks to an HTML textarea with JavaScript.

In this article, we’ll look at how to add line breaks to an HTML textarea with JavaScript.

How to add line breaks to an HTML textarea with JavaScript?

To add line breaks to an HTML textarea with JavaScript, we add '\r\n' at the end of the string.

For instance, we write

const txtArea = document.getElementById("txtDebug");
txtArea.value += text + "\r\n";

to select the text area with getElementById.

And then we append "\r\n" after text to add a new line after text and set that as the value of the text area.

Conclusion

To add line breaks to an HTML textarea with JavaScript, we add '\r\n' at the end of the string.

Leave a Reply

Your email address will not be published. Required fields are marked *