The code below aims to check a string length of a given html element.
<span class="subject">subject abcd</span>
It truncates it if the string is longer than 130 characters, then the string is displayed with three dots at the end of it. What if I have more than ten span elements named .subject, is there a way to make a 'lazy' function to check all of them and truncate only those that are longer than the 130 characters?
vanilla Javascript code:
var res, nodeLength = document.querySelector(".subject").innerHTML;
if(nodeLength.length > 130){
res = nodeLength.slice(0,130);
document.querySelector(".subject").innerHTML = res + "...";
}
HTML code:
<div class="item">
<div class="article">
<span class="subject">subject abcd</span>
<span class="time">time</span>
<span class="text">text</span>
</div>
<a href="#" class="more">more</a>
</div>