JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id="mytext" cols="10" rows="5" wrap="off"></textarea>
<a href="#" onclick="alert( $('#mytext').val() )">Test value</a>
JavaScript
$(document).ready(function() {
textarea = $('#mytext');
textarea.keyup(insertNewlines);
});
function insertNewlines(e)
{
// Get the number of cols defined (where we will insert breaks manually
var rowMaxChars = textarea.attr('cols');
// Calculate the row we were on as newlines + 1
var rowNum = (textarea.val().match(/\n/g) != null) ? textarea.val().match(/\n/g).length + 1 : 1;
// If the chars in textarea are greater than rowMaxChars * rowNum then insert newline
if( textarea.val().length > rowMaxChars*rowNum && e.keyCode != 8 )
{
textarea.val( textarea.val() + "\n" );
}
return true;
}