Val Do
Val Do

Reputation: 2695

How to remove words from string using jquery?

I need to remove characters from string if characters number > than 10 and insert three dots ...

I need remove characters
I need remov...//like this text

Upvotes: 1

Views: 212

Answers (1)

Nate Barbettini
Nate Barbettini

Reputation: 53600

As Roy mentioned, you don't need jQuery to do this. Plain JavaScript works just fine.

Try:

if (str.length > 10)
    str = str.substring(0, 10) + '...'

Here's a fiddle.

Another way is to use the ternary operator. It's shorter but not as readable if you aren't used to the ternary syntax:

str = (str.length > 10) ? str.substring(0, 10) + '...' : str;

This is equivalent to the if statement above.

Upvotes: 3

Related Questions