X

LATEST #JQUERY NEWS

How to Dynamically Trim Text Using jQuery


This easy beginners jQuery tutorial demonstrates how you can use jQuery to trim the text length of an HTML element based on how many characters you wish to be viewable by the user. With this straightforward and simple code snippet, you’ll be able to trim your text and then add an ellipses (…) to the place where the text cuts off, so that your user knows they’re not seeing the full text, but a shortened version of it. Thanks to jQuery, all of this can be done dynamically.

To take a look at how you’d implement this sort of effect in your own code, check out the code snippet below:

$(document).ready(function($){
 $("p").each(function(){
 if ($(this).text().length > 40) {
 $(this).text($(this).text().substr(0, 37));
 $(this).append('...');
 }
 });
});

In the snippet above, each of the “p” elements are checked for length. If the length is greater than 40 characters, the text is trimmed so that only characters 0-37 are visible to the user. This is done using the .substr() method in conjunction with the .text() method. Finally, using the .append() method, the ellipses symbol (…) is added at the end of the newly shortened text block. If the text is under 40 characters, then this code snippet won’t apply, and no changes will be made.

This type of code snippet is perfect for shortening HTML text elements that take up too much space within their parent element or designated div. Especially when it comes to mobile view, sometimes the space an element is meant to take up just becomes to small to accommodate it, an this jQuery snippet is a great way to dynamically manage text overflow problems without having to alter the HTML or CSS files. Of course, there are other options to fix this issue as well, but this one offers a quick fix and is an easily implementable solution that should work on just about any of your projects.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...