jschouwstra
12/6/2017 - 4:07 PM

Text-excerpt

Excerpt text if text is longer than x

<html>
<head>

</head>

<body>
<ol>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-5">123</span></a></span></li>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-6">123456789</span></a></span></li>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-5">123456789</span></a></span></li>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-5">123456789</span></a></span></li>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-5">123456789</span></a></span></li>
	<li><span class="text-excerpt"><a href="google.com"><span class="limit-5">123456789</span></a></span></li>
</ol>
<script
  src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
  integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
  crossorigin="anonymous"></script>
  <script type="text/javascript">
  


  $(document).ready(function(){
	$('.text-excerpt .limit-5').each(function(){
	var limit = 5;
	console.log($(this).html().length);
		//If string length higher longer than 3
		if($(this).html().length > limit){
			
			//Shorten string to 5 characters
			$(this).text($(this).text().substring(0,limit) + "...");
			//alert("higher than zero");
		}
	});
	
  	$('.text-excerpt .limit-6').each(function(){
	
	var limit = 6;//x
	console.log($(this).html().length);
		//If string length longer than x
		if($(this).html().length > limit){
			
			//Shorten string to x characters
			$(this).text($(this).text().substring(0,limit) + "...");
			//alert("higher than zero");
		}
	});
  
  });
  
  </script>
</body>
</html>