Limit characters and display points

Asked

Viewed 111 times

1

I have the following code:

if (i.link[E].href == "teste") {
 var u = i.link[E].type.substring(0,130) + "...";
}

It limits the characters to 130, and displays the three dots at the end of the text. However, the dots are displayed even if there are no more than 130 characters, so it makes no sense to have 50 characters and have three dots at the end. How do I display the points only if the 130 limit is reached?

  • why don’t you add i.link[E].type.length > 130 in your if? it only applies if the content is more than 130...

  • I got it. I believed it was really simple, but I’m a beginner in js. Thank you.

1 answer

1


Put the size validation on if; so, only enter if the href == teste and the size of strin is greater than 130:

var u = i.link[E].type;

if (i.link[E].href == "teste" && u.length > 130) {
   u = u.substring(0,130) + "...";
}

Detail: if you only have 130 characters to display, remember take the three points out of the account, that is, it would count the size of the string in 127.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.