1

I am trying to convert this function but I can't seem to translate a few of the lines successfully.

$('.text').each(function() {
  str = String($(this).html()); #this line
  tot = str.length;
  str = (tot <= max) ? str : str.substring(0,(max + 1))+"..."; #this line
  $(this).html(str); #this line
});

How can I convert this into a javascript function?

0

2 Answers 2

1
document.querySelectorAll('.text').forEach(function(el){
var str = el.innerHTML;
var tot = str.length;
...
this.innerHTML = str;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try with this:

document.getElementsByClassName('text').forEach(el => {
   str = el.innerHTML;
   tot = str.length;
   str = (tot <= max) ? str : str.substring(0,(max + 1))+"..."; 
   el.innerHTML = str;
})

1 Comment

Don't use getElementsByClassName unless you explicitly want an HTMLCollection instead of a NodeList.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.