Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace extra texts with (....) using javascript

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?

like image 457
R. David Avatar asked Dec 03 '25 21:12

R. David


2 Answers

$('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

like image 51
adeneo Avatar answered Dec 05 '25 10:12

adeneo


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

like image 26
George Avatar answered Dec 05 '25 10:12

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!