jQuery for Characters Limit

When you have a div that has too much characters and want to limit the amount of characters that show up in there to 35 characters. You will need to write simple jQuery about it.

<!-- HTML div -->
<div id="mydiv">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
</div>
 
 <!-- jQuery -->
<script>
jQuery(document).ready(function(){
	jQuery("#mydiv").each(function() { 
		var title_text=jQuery(this).text();
		var dots = "...";
		if(title_text.length > 35)
		{
			title_text = title_text.substring(0,35) + dots;
		}
		jQuery(this).html(title_text);
	});
});
</script>

Result

Lorem Ipsum is simply dummy text of…

You can change character limit as per your requirement & remove ellipses from the end if desired.
Ellipsis shows to your user that the text was shortened. So we would recommend it.

Leave a Reply

Your email address will not be published. Required fields are marked *