My goal was to truncate a string and add an ellipsis at the end using jQuery, but my code doesn't seem to be working as intended.
Below is the jQuery code I wrote:
$(".link-content > h4").each(function(){
var len = $(this).text().length;
var str = $(this).text().substr(0, 26);
var lastIndexOf = str.lastIndexOf();
if(len > 26){
$(this).text(str.substring(0, lastIndexOf) + "...");
}
});
<div class="links">
<span>FACEBOOK</span>
<div class="link-content">
<h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam lacus tellus, tristique congue pellentesque ac, semper eu
d iam.</h4>
</div>
</div>