Home > Back-end >  Truncate string text using jquery
Truncate string text using jquery

Time:10-13

I have a Razor view Html.DisplayFor where I am displaying some text. Now I want to truncate the text using jquery. I am using the jquery code from this No console errors

CodePudding user response:

If you are using Layout = null; then @section Scripts doesn't work because the section is defined in _Layout.cshtml. You have to add a full html document in your view.

@{
    Layout = null;
}

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>title</title>
  </head>
  <body>
    <div >
      <p >@Html.DisplayFor(modelItem => item.BlogContent)</p>
    </div>

    <script src="~/js/jquery.min.js"></script>
    <script src="~/bootstrap/js/bootstrap.min.js"></script>
    
    <script>
      $(document).ready(function() {
        $('ul.pagination > li.disabled > a').addClass('page-link');
        truncateText(".p-bottom-20", 100);
      });

      function truncateText(selector, maxLength) {
        $(selector).text((i, txt) => txt.length > maxLength ? txt.substr(0, maxLength)   "..." : txt);
      }
    </script>
  </body>
</html>
  • Related