Bernd Wechner
Bernd Wechner

Reputation: 2143

Client Side Javascript: Is it possible toget the laid out text rendition of an HTML element in the DOM?

For any HTML element we can easily get it's outerHTML, innerHTML and textContent as properties.

Console based browsers, like lynx and elinks, produce fair approximations of intended HTML layout in text form. I wonder if there's a DOM method or a Javascript library that can offer the same to client-side Javascript.

My main interest is in preserving table layout.

I've search rather extensively on-line for possible solutions, but this alas, is one of those problems that is hard to describe well, or better said I'm not familiar with an established jargon or language that describes the desired task here. The best I can come up with the title of this question "get the laid out rendition of an HTML element".

Upvotes: 0

Views: 59

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370789

innerText will get you somewhat close to what you want:

The innerText property of the HTMLElement interface represents the "rendered" text content of a node and its descendants.

As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard.

Note: innerText is easily confused with Node.textContent, but there are important differences between the two. Basically, innerText is aware of the rendered appearance of text, while textContent is not.

For the vast majority of DOM operations, textContent or innerHTML should be preferred, but this is one of the few times innerText could be warranted.

Upvotes: 1

Related Questions