Javascript DOM Textarea Split text by line

Description

Javascript DOM Textarea Split text by line

View in separate window

<!DOCTYPE html>
<head>
<script>

window.onload=function() {//from  ww  w  .  j a  va 2 s . com
 document.getElementById("preview").onclick=processText;
}

function processText() {
 let txtBox = document.getElementById("inputbox");
 let lines = txtBox.value.split("\n");

 // generate HTML version of text
 let resultString  = "<p>";
 for (let i = 0; i < lines.length; i++) {
   resultString += lines[i] + "<br />";
 }
 resultString += "</p>";

 // print out to page
 let   blk   = document.getElementById("result");
 blk.innerHTML  =  resultString; 
}

</script>

</head>
<body>
<textarea id="inputbox" cols="20" rows="10"></textarea>
<div><button id="preview">Preview</button></div>
<div id="result"></div>
</body>
</html>



PreviousNext

Related