1

I'm pretty new to both JavaScript and HTML, so I was just wondering if there was a way to access the text stored within specific rows of a text area. Here is my sample textarea:

<body>
   <textarea id = 'typeHere' rows = "5" cols = "60"></textarea>
</body>
Paperpal9
  • 11
  • 2

2 Answers2

1

You can get the text and split it into lines:

 var text = document.getElementById("typeHere").value;
 var lines = text.split("\n");

The variable lines is an array of strings.

Rediska
  • 1,392
  • 10
  • 14
  • I tried something like this to test it out, but for some reason the console.log prints "test undefined" ```var text = document.getElementById("typeHere").value; var lines = text.split("\n"); console.log("test", lines[1]); ``` (Sorry for the weird formatting, I don't really know how to use this) – Paperpal9 May 15 '20 at 03:54
  • Actually I think I got it, thanks – Paperpal9 May 15 '20 at 04:04
1

Get value of textarea, split on lines by \n and then take row. for example:

document.querySelector("#wmd-input").value.split('\n')[0]

"example"

artem
  • 457
  • 4
  • 5