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 & gt; 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
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...

article image
»

Check all checkboxes in HTML table using jQuery

HTML tables are a very handy and useful element. They can adapt to ...

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

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 ...