Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only the ellipsis text using jquery

Nice code, just wondered if it is possible to query and get the ellipsis text (i.e. with the dots in and not the original text)?

If I add the text

This is a long sentence

and (using the relevant css for ellipsis) it gets shortened to

This is a long sen ...

Is there a way to get the text

"This is a long sen ..." 

from the $("p") DOM object rather than the original text?

like image 585
ejectamenta Avatar asked Nov 11 '22 14:11

ejectamenta


1 Answers

Try that:

function getEllipsis(command, characters) {
	for (var i = command.length; i >= 0; i--) {
		if (command.substring(0, i).length < characters) {
			if (i < command.length) {
				command = command.substring(0, i) + "...";
			}
			return command;
		}
	}
}
console.log(getEllipsis("I am a long sentence",16))
console.log(getEllipsis("But I am even longer",20))
like image 160
Woold Avatar answered Nov 14 '22 22:11

Woold