Zhihar
Zhihar

Reputation: 1428

how get text with break line from element (jquery)

All!

I need to get the text from the element wit break lines. When I use the text() jQuery command, I get the text without break lines. However, in the properties (innerText, outerText) of the elements, I see the currect text.

For example:

<table><tr><td id = 'element'>word#1<br>word#2</td></tr></table>

Properties (in command line in Chrome) I see:

innerText: word#1 -> word#2
outerText: word#1 -> word#2

where -> - break line

But when I use text(), I get

word#1word#2

Tell me please how currect get text with break line

Upvotes: 1

Views: 2808

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68635

You need to get via .html() to get also the <br>s. After it you can replace the <br>'s with break lines.

Consider you already has the text with <br>'s. Split it with <br> and join with \n.

var text = "asd<br>asd<br>asd";
console.log(text);
var withBreakLines = text.split('<br>').join('\n');
console.log(withBreakLines);

Upvotes: 4

Related Questions