Add Read More text to the String using JQuery

Leave a Comment
Hi All,
In a following post we will see about how to add “Read More” text using JQuery.Please find the below code Snippet.

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">

    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            var showChar = 200;
            var ellipsestext = "...";
            var moretext = "Read More";
            var lesstext = "less";
            $('.moreNew').each(function () {
                var content = $(this).html();
                if (content.length > showChar) {
                    var c = content.substr(0, showChar);
                    var h = content.substr(showChar - 1, content.length - showChar);
                    var html = c + '<span class="moreellipses">' + ellipsestext +
                    ' </span><span class="morecontentNew"><span>' + h +
                     '</span>  <a class="morelinkNew" href=""; color: red>' + moretext + '</a></span>';
                    $(this).html(html);
                }
            });
            $(".morelinkNew").click(function () {
                if ($(this).hasClass("less")) {
                    $(this).removeClass("less");
                    $(this).html(moretext);
                }
                else {
                    $(this).addClass("less");
                    $(this).html(lesstext);
                }
                $(this).parent().prev().toggle();
                $(this).prev().toggle();
                return false;
            });
        });
    </script>
    <style type="text/css">
        a a:visited
        {
            color: #0254EB;
        }
        a.morelinkNew
        {
            text-decoration: none;
            outline: none;
        }
        .morecontentNew span
        {
            display: none;
        }
        .commentNew
        {
            width: 430px;
            background-color: #f0f0f0;
            margin: 10px;
        }
    </style>
</head>
<body>
    <font face="Arial Black, Arial, sans-serif">
        <div class="commentNew moreNew">
            It is a long established fact that a reader will be distracted by the readable content
            of a page when looking at its layout. The point of using Lorem Ipsum is that it
            has a more-or-less normal distribution of letters, as opposed to using 'Content
            here, content here', making it look like readable English. Many desktop publishing
            packages and web page editors now use Lorem Ipsum as their default model text, and
            a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
            versions have evolved over the years, sometimes by accident, sometimes on purpose
            (injected humour and the like).</div>
</body>
</html>

We can see the output as follows,



Thanks.

Related Post

0 comments:

Post a Comment