Jquery

jQuery Add More or Less Link into Paragraph

Today’s topic is jQuery Add More or Less Link into Paragraph. There are many plugins that are used to show more and less link on the paragraph.

We are showing more and less link on paragraphs using jquery core which saves you loading plugin time when you will use a plugin for this work.

This is a simple three steps coding to achieve this wonderful functionality. We are using jQuery to show/hide text and links.

You can also check other tutorials of dynamic read more Using Server side PHP and Mysql

We will create a paragraph in HTML in a div.

Related Post
<div class="comment more">consectetur adipiscing elit. Proin blandit nunc sed sem dictum id feugiat quam blandit. Donec nec sem sed arcu interdum commodo ac ac diam. Donec consequat semper rutrum. Vestibulum et mauris elit. Vestibulum mauris lacus, ultricies.</div>

Now we will define the CSS class for a link.

a {  
    color: #0254EB  
}  
a:visited {  
    color: #0254EB  
}  
a.morelink {  
    text-decoration:none;  
    outline: none;  
}  
.morecontent span {  
    display: none;  
}  
.comment {  
    width: 400px;  
    background-color: #f0f0f0;  
    margin: 10px;  
}

jQuery code for more and less link on click event.

$(document).ready(function() {  
    var showChar = 100;  
    var ellipsestext = "…";  
    var moretext = "more";  
    var lesstext = "less";  
    $('.more').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 + '' + ellipsestext+ ' ' + h + '  ' + moretext + '';  
  
            $(this).html(html);  
        }  
  
    });  
  
    $(".morelink").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;  
    });  
});

I hope this helps you.

You can download the source code and see Demo from the below link.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories