Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text when it's out of its own div?

I am wondering if there is any way with Javascript to get a text when it's out of its own <div>. For example if a text is made of 3 sentences, and the last one is only halfy displayed, is there a way to get the entire last sentence, or the first word that isn't displayed with JavaScript ?

Example :

HTML

<div>loooooong loooooong text. Second sentence. Third sentence</div>

CSS

div {
  max-width: 60px;
  max-height: 50px;
  overflow: hidden;
  text-overflow: ellipsis;
}
like image 930
br.julien Avatar asked Dec 10 '25 11:12

br.julien


1 Answers

To get all the text:

var node = document.getElementsByClass('div')[0];
var fulltext = node.textContent; // gets all textual content, hidden or not

To get the text displayed:

var vistext = node.innerText; // gets only text displayed on page (despite my comment!)

Hidden text is then fulltext - vistext

var hiddentext = fulltext.substr(vistext.length);

Should do the trick...

like image 133
allnodcoms Avatar answered Dec 12 '25 03:12

allnodcoms