Is there a way to retrieve truncated text from a node using Javascript? Take a look at the code snippet below:
console.log(document.getElementById("text1").textContent);
// prints "This is a long text"
console.log(document.getElementById("text2").textContent);
// prints "This is a long text as well"
// expected "This is a long text a..."
.longtext {
width: 140px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div id="text1">This is a long text</div>
<div class="longtext" id="text2">This is a long text as well</div>
I am looking for a solution to obtain the desired "This is a long text a..."
from the element with id text2
.