Timmy Von Heiss Timmy Von Heiss - 1 year ago 47
jQuery Question

using substr to limit characters inside of an element

I am paginating users and in my view I have:

<li class="profile">
<%= user.profile %>

I want to limit
to 200 characters followed by three dots

Below does not work:

if ($(this).html().length > 200) {
$(this).html($(this).html().substr(0, 200));

from developer tools

this is the way it looks in developer tools.


li.profile {
font-size: 14px;
padding-bottom: 5px;
text-align: left;
padding-left: 5px;

Answer Source

Use .text(); .html() sets .innerHTML of element, not .textContent

       if (this.textContent.length > 200) {
           $(this).text(function(_, text) {
             return text.substr(0, 200).concat("...");