i have a text filed where users write something and i get the text with javascript of that text box and show it in a paragraph.I have text limit for the paragraph like 50 chrac. If users write more than 50 chrac i wanna show (...) after 45 chrac. is it possible using javascript?
Like this is the paragraph what user wrote and its more than 50 chrac.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
i wanna show this as
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt (.....)
is it possible using javascript?
$('textarea').on('keyup', function() {
var v = this.value.length > 50 ? this.value.substr(0,45) + '(...)' : this.value;
$('#result').text( v );
});
FIDDLE
Here's how you'd cut it off at the last word:
FIDDLE
The following will append (...) after 45 characters of the text if it is more than 50 characters long, for each element in the set.
elem = $('p');
elem.each(function(){
curTxt = $(this).text();
$(this).text(curTxt.length > 50 ? curTxt.substring(0,45)+'...' : curTxt);
});
JSFiddle
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