How to create website read more and show more button ?

          How to create read more and show more button ?

To create a "Read More" or "Show More" button for expanding content on a web page, you can use this example code to get you started:

Body:

<div id="content">

  <p> This is the initial content that is visible. It can be as long or as short as you want</p>

  <p class="hidden">This is the additional content that will be hidden initially</p>

  <button id="showMoreBtn">Read More</button>

</div>

<style>

.hidden {

  display: none;

}

</style>

 <script type='text/javascript'>

document.addEventListener("DOMContentLoaded", function() {

  var showMoreBtn = document.getElementById("showMoreBtn");

  var hiddenContent = document.querySelector(".hidden");

  showMoreBtn.addEventListener("click", function() {

    if (hiddenContent.style.display === "none") {

      hiddenContent.style.display = "block";

      showMoreBtn.innerHTML = "Show Less";

    } else {

      hiddenContent.style.display = "none";

      showMoreBtn.innerHTML = "Read More";

    }});

});

</script>

In this example, the initial content is wrapped in a <div> with the id="content". The additional content that will be hidden is placed inside a <p> element with the class="hidden". The "Read More" button has the id="showMoreBtn".

The CSS sets the display property of elements with the hidden class to "none" initially, hiding them from view.

The JavaScript code adds a click event listener to the "Read More" button. When the button is clicked, it checks the current display state of the hidden content. If it is hidden (display: none), it sets the display to "block" to show the content and updates the button text to "Show Less". If the content is already visible, it hides it again (display: none) and changes the button text back to "Read More".

You can customize the HTML, CSS, and JavaScript code according to your specific requirements and apply styles to match your website's design.



Online Source

Comments

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources