I am trying to make a fragment of all h3 text somehow using some array methods. But the problem is to select all h3 that I used as a selector
var productTitle = $('#products-carousel').find('.single-trending-list').find('h3').text();
And it combines all h3 texts, and finally I get the same text for all h3 texts
How can I highlight all h3 separately?
Here is the JSFiddle
Here is my code:
var productTitle = $('#products-carousel').find('.single-trending-list').find('h3').text(), productTitleExcerpt; var toArray = productTitle.split("", 36), joinArray = toArray.join(''), joinArrayToArray = joinArray.split(" "), joinArrayToArrayPop = joinArrayToArray.pop(), joinArrayToArrayPopPush = joinArrayToArray.push('...'), joinArrayToArrayPopPushJoin = joinArrayToArray.join(' '), productTitleExcerpt = joinArrayToArrayPopPushJoin; $('#products-carousel').find('.single-trending-list').find('h3').text(productTitleExcerpt);
<ul class="ul-trending-list" id="products-carousel"> <li> <div class="single-trending-list"> <span>$90.00</span> <h3>Leather Clutch, Gray Distressed</h3> </div> </li> <li> <div class="single-trending-list"> <span>$91.00</span> <h3>Sori Yanagi Butterfly Y Wenge Stool</h3> </div> </li> <li> <div class="single-trending-list"> <span>$31.00</span> <h3>BOTTLIT Canister, 600ml</h3> </div> </li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
I made a DECISION . Now I used each method and also created a function and set 3 arg to pass the element , letter number , extension . So now I can use it anywhere.
Here is the JSFiddle
And here is the code you can see here.
var originalCarouselH3 = $('#products-carousel').find('.single-trending-list').find('h3'); function excerpt(excerptElement, number , more = "..."){ excerptElement.each(function(){ var productTitle = $(this).text(), productTitleExcerpt, toArray = productTitle.split("", number), joinArray = toArray.join(''), joinArrayToArray = joinArray.split(" "), joinArrayToArrayPop = joinArrayToArray.pop(), joinArrayToArrayPopPush = joinArrayToArray.push(more), joinArrayToArrayPopPushJoin = joinArrayToArray.join(' '), productTitleExcerpt = joinArrayToArrayPopPushJoin; if(productTitle.length > number){ productTitle = productTitleExcerpt; $(this).text(productTitle); } }); } excerpt(originalCarouselH3, 30);
<ul class="ul-trending-list" id="products-carousel"> <li> <div class="single-trending-list"> <span>$90.00</span> <h3>Leather Clutch, Gray Distressed</h3> </div> </li> <li> <div class="single-trending-list"> <span>$91.00</span> <h3>Sori Yanagi Butterfly Y Wenge Stool</h3> </div> </li> <li> <div class="single-trending-list"> <span>$31.00</span> <h3>BOTTLIT Canister, 600ml</h3> </div> </li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
source share