AB hover preview test: Truncate text script
require(['jquery', 'domReady'], function ($, domReady) { | |
domReady(function () { | |
var elem = document.querySelectorAll('.product-addl-info-details:not(.nf-active)'); | |
var $productList = $('#product-listing'); | |
if ($('.product-addl-info-details.nf-active').length === 0) { | |
$productList.on('click', '.product-addl-info-details', function (event) { | |
var link = $(event.target).parents('.product-block-js').find('a.product-block-pdp-url').attr('href'); | |
window.location = link; | |
}); | |
} | |
var truncate = function (elem, limit) { | |
// Make sure an element and number of items to truncate is provided | |
if (!elem || !limit) return; | |
elem.classList.add("nf-active"); | |
// Get the inner content of the element | |
var content = elem.textContent.trim(); | |
var arrWordLength; | |
// Convert the content into an array of words | |
// Remove any words above the limit | |
content = content.split(' '); | |
arrWordLength = content.length; | |
content = content.slice(0, limit); | |
// Convert the array of words back into a string | |
content = content.join(' '); | |
var link = document.createElement('p'); | |
var node = document.createTextNode('Learn More'); | |
link.appendChild(node); | |
link.classList.add("m_learn_more"); | |
//if our word link is greater than 11 words add an ellipsis to the end | |
if (arrWordLength > 11) { | |
content = content + '...'; | |
} | |
elem.textContent = content; | |
// Append updated link back into DOM | |
elem.appendChild(link); | |
}; | |
for (var i = 0; i < elem.length; i++) { | |
truncate(elem[i], 24); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment