Home > Net >  Limiting characters in my description to 140 characters with javascript
Limiting characters in my description to 140 characters with javascript

Time:06-25

I'm working on a project that requires a list of books to be printed onto the page using an api url.

Code works wonders, but I'm struggling to limit the characters to 140 for the description as requested.

function renderItemHtml(item) {
return `<div ><img src="${item.volumeInfo.imageLinks.thumbnail}"  
alt="${item.singleTitle} by ${item.volumeInfo.authors[0]}" />
<div>
<h4>${item.volumeInfo.title}</h4>
<p><strong>${item.volumeInfo.authors}</strong></p>
<h8>${item.volumeInfo.description}</h8>        <-------- what i need to limit
</div>
<br>
<h8>Pages:  ${item.volumeInfo.pageCount}</h8>
</div>`

}

CodePudding user response:

Just do this:

<h8>${item.volumeInfo.description.slice(0,140)}</h8> 
  • Related