Limit characters in div and display points only if necessary?

Asked

Viewed 83 times

1

I got the following:

$("h2").text(function(index, currentText) {
 return currentText.substr(0, 100) + '...';
});

It limits the text of the H2 tag to 100 characters and displays the three dots at the end, however the dots are displayed even if the characters do not reach 100 characters, so it makes no sense to have the dots if the title is already complete. How can I display the points only if H2 exceeds the limit?

1 answer

2


How can I display the points only if H2 is going to exceed the limit?

Just add the condition of only executing the code if the string is greater than 100 characters, agree?

$("h2").text(function(index, currentText) {
    if(currentText.length > 100)
        return currentText.substr(0, 100) + '...';

    return currentText;
});

I believe this answers the question, I don’t know what the purpose of your code is, but maybe what you really need is to use Ellipsis.

Browser other questions tagged

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