Debug School

rakesh kumar
rakesh kumar

Posted on

How to limit description length using for loop in jquery

step 1: get data from server side

var describe = data.description;
Enter fullscreen mode Exit fullscreen mode

step2: define description threshold value to limit char

var descriptionTab = $('#description');
var descriptionThreshold = 180;
Enter fullscreen mode Exit fullscreen mode

step3:if description length less than description threshold value display data in single line

if (describe.length <= descriptionThreshold) {
    // If the description is short, append it directly
    descriptionTab.append('<span><b>Task Description: </b>' + describe + '</span>');
}
Enter fullscreen mode Exit fullscreen mode

Image description

step4: if description length greater than description threshold value display data in multiple line

else {
    // If the description is too long, break it into chunks
    descriptionTab.append('<span><b>Task Description: </b></span>');

    for (var i = 0; i < describe.length; i += descriptionThreshold) {
        var chunk = describe.substring(i, i + descriptionThreshold);
        descriptionTab.append('<div>' + chunk + '</div>');
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)