ThisaruG
ThisaruG

Reputation: 3412

how to retrieve a line of text from ACE editor?

I integrated ACE editor in my site. I want to retrieve the text in a certain line. I've searched and found This and This. But unfortunately, I can't understand those, as I'm a newbie to the subject. Can anyone give an example for it ? I got the lines to an array using the following code.

var line = document.getElementsByClassName("ace_line");

Are there any methods like

line[count].getValue

Which can return the string of the text in that line ?

Upvotes: 1

Views: 1601

Answers (2)

Sam Battat
Sam Battat

Reputation: 5745

line[count] already returns the DOM element of that line. So all you have to do is to extract the plain text from that element. if you are using jQuery you could do:

To get the text of the first line for example (line[0])

var lineText = line[0].text();

If you are not using jQuery and want to do that in javascipt:

var lineText = line[0].innerHTML.replace(/<[^>]*>/g, "");

Upvotes: 0

a user
a user

Reputation: 24159

You need to call

line0 = editor.session.getLine(0);

using getElementsByClassName("ace_line") or similar won't work since

  1. ace creates DOM elements only for visible lines.
  2. text content of the DOM element is different from the text in document since tabs and some other characters are replaced by spaces.

Upvotes: 4

Related Questions