Categories
JavaScript Answers

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 can add '\r\n' to the string.

For instance, we write

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

to select the text area with getElementById.

And then we append text + "\r\n" to the value property.

"\r\n" adds the line breaks.

Conclusion

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

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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