Adding Readmore and Readless toggle button to website
readmore and readless toggle button
In this blog we will learn how to add a readmore and readless toggle button for webpage using Javascript.
The 'read more' button comes in handy when there is a more text and want to show some part of the text.
This is common in several websites like blogging sites, or when showing in profile description or to show course topics etc.
There will be a button, toggles between showing and hiding content as and when user clicks on.
By default, it is collapsed and shows ellipsis(..). When button clicked, it expands the content. As shown in the below picture.
Example code:
<!DOCTYPE html> <html> <head> <title>e-Trainings Demo</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <style> #more {display: none;} </style> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <h4><small>RECENT POSTS </small></h4> <hr> <h2>Read More Read Less Button</h2> <h5><span class="glyphicon glyphicon-time"></span> Post by Soma, june 27, 2021.</h5> <h5><span class="label label-danger">Techonology</span> <span class="label label-primary">Ipsum</span></h5><br> <p>Techonology is my passion. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Excepteur sint occaecat cupidatat <span id="dots">...</span><span id="more">non proident, sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</span></p> <button onclick="myFunction()" id="myBtn">Read more</button> <br><br> <hr> </div> </div> <script> function myFunction() { var dots = document.getElementById("dots"); var moreText = document.getElementById("more"); var btnText = document.getElementById("myBtn"); if (dots.style.display === "none") { dots.style.display = "inline"; btnText.innerHTML = "Read more"; moreText.style.display = "none"; } else { dots.style.display = "none"; btnText.innerHTML = "Read less"; moreText.style.display = "inline"; } } </script> </body> </html>
Comments
Post a Comment