JSFiddle - React, Tailwind, and code Playground

by cferdinandi

HTML

<div class="truncate">
	Port tender gun spanker lanyard heave to topmast. Heave down draught piracy loaded to the gunwalls mizzenmast topsail Brethren of the Coast. Lanyard snow Jack Ketch swing the lead maroon spike black jack.
</div>

JavaScript

var truncate = function (elem, limit) {

	// Make sure an element and number of items to truncate is provided
	if (!elem || !limit) return;

	// Get the inner content of the element
	var content = elem.textContent.trim();

	// Convert the content into an array of words
	content = content.split(' ');
	console.log(content);
	
};

var elem = document.querySelector('.truncate');
truncate(elem, 7);