I am working with an HTML file that contains a textarea tag. My goal is to copy and paste text with multiple lines into the textarea and then use JavaScript to extract the last three lines into three separate variables. The textarea has the id "txt" assigned to it. The function "clicked" is assigned to a button in the HTML file.
function clicked(){
var txt = document.getElementById("txt");
var original = txt.value; //original text transferred to original variable
var lastline = //store the last line in this variable
var secondlast = //store the second to last line in this variable
var thirdlast = //store the third to last line in this variable
var modified = //store text without these three lines in this variable
console.log(lastline); //how are you
console.log(secondlast); //now is the time
console.log(thirdlast); //helloo there
console.log(modified); //school data is the important
//checking home
//not there
}
Text inputted in textarea:
school data is the important
checking home
not there
helloo there
now is the time
how are you
Output:
how are you
now is the time
helloo there
school data is the important
checking home
not there