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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With