Textarea automatic line break

6.1k views Asked by At

I have a textarea on my site where I want to paste URLs.

Is this possible to create line break every time I paste URL? If not, can it create line break when I enter space?

I've searched for the solution but all i found is the solution to create line breaks after form submit which didn't help me.

2

There are 2 answers

0
Stephen Brown On BEST ANSWER

Fiddle here: https://jsfiddle.net/3sj2644z/

this.value = this.value + "\n";

You listen for the paste event on the textarea and you grab the text that is currently in it and append a linebreak with the escape character \n to it and then place that new string value back into the textarea.

You don't use html in the textarea so the br tag doesn't work if you think so.

1
Cherryishappy On

function ConvertToLinks() {
  str = document.getElementById("S1").value;
  str = str.replace(/\r\n|\n/g,'<br>');
  document.getElementById('txtLinks').innerHTML = str;
}
<html>
<head>
<title>Text area redirect</title>

</head>
<body>
<textarea rows="5" id="S1" name="S1" cols="40">
<a href="http://yahoo.com">Yahoo</a>
<a href="http://google.com">Google</a>

<a href="http://webdeveloper.com">Web Developer</a>
<a href="http://www.codingforums.com/javascript-programming/195565-place-links-textarea.html">from Web</a>
</textarea>
<br><button onclick="ConvertToLinks()">Convert to Links</button>
<div id="txtLinks" style="width:350px;min-height:100px;border:1px solid red"></div>
</body>
</html>
By http://www.codingforums.com/javascript-programming/195565-place-links-textarea.html