I know there are lots of truncate scripts out there, but I can't use most of them due to integration issues with the cms I am working on.
Basically I must do it this way:
Being terrible at javascript here is my lame non-working attempt:
if ($('div.text').val().length > 10) {
//
($('div.text').append('...');
}
Can someone please help?
text-overflow: ellipsis; white-space: nowrap; overflow: hidden; in CSS (or hard-code the style, but CSS is cleaner). If you want to completely cut the text off, use clip instead of ellipsis .
The <sup> tag defines superscript text. Superscript text appears half a character above the normal line, and is sometimes rendered in a smaller font.
Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.
To select all text inside an element such as DIV, we can simply use JavaScript document. createRange() method to create a range, and than using range. selectNodeContents() we can set node range (start and end), after which use selection. addRange() to select the range of the element.
if ($('div.text').text().length > 10)
or
if ($('div.text').html().length > 10)
div elements don't have a "value" as returned by val(), but they do have text or html
and then you probably want to truncate the text like
var text = $('div.text').text();
text = text.substr(0,10) + '...';
$('div.text').text(text);
You're looking for the CSS property text-overflow: ellipsis. This is supported by most browsers (even IE7+), but Firefox only supports it as of version 7. For older browser support, you can use some existing jQuery plugins.
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