Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textContent() without spaces from formatting text

Tags:

javascript

I have an html like this:

<div id="info">
    <h1> My title </h1?
    <div>
        <p> Here is a text and one paragraph </p>
        <p> Another paragraph </p>
    </div>
</div>

I would like to take the inner text without the tags from the whole div#info. So I use this:

document.querySelector('div#info').textContent

And the results have some spaces like this:

"
    My title




Here is a text and one paragraph
Another Paragraph




"

Any idea if there is any command which could give the result to so one line like this:

 "My title Here is a text and one paragraph Another Paragraph"

I tries also the innerText() but it has again spaces.

like image 831
Keri Avatar asked Mar 21 '17 07:03

Keri


People also ask

What is the difference between textContent and innerText?

textContents is all text contained by an element and all its children that are for formatting purposes only. innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.

What is textContent in HTML?

The textContent property in HTML is used to set or return the text content of the specified node and all its descendants. This property is very similar to nodeValue property but this property returns the text of all child nodes. Syntax: It is used to set the text of node.

What does the innerText give you?

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.


1 Answers

You need a regex to remove all of the newlines and excess whitespace:

.replace(/[\n\r]+|[\s]{2,}/g, ' ')

Then you can .trim() the result of that call:

console.log(
  document.querySelector('div#info')
          .textContent
          .replace(/[\n\r]+|[\s]{2,}/g, ' ').trim()
)
<div id="info">
    <h1> My title </h1>
    <div>
        <p> Here is a text and one paragraph </p>
        <p> Another paragraph </p>
    </div>
</div>
like image 119
Rob M. Avatar answered Oct 24 '22 08:10

Rob M.