---
title: "Create Read More / Read Less Functionality for Blog Posts in JavaScript?"  
description: "Create Read More / Read Less Functionality for Blog Posts in JavaScript?"  
author: "Rahul Roi"  
published: 2021-04-05  
updated: 2023-11-29  
canonical: https://www.mindstick.com/forum/156449/create-read-more-read-less-functionality-for-blog-posts-in-javascript  
category: "javascript"  
tags: ["javascript", "angular js"]  
reading_time: 2 minutes  

---

# Create Read More / Read Less Functionality for Blog Posts in JavaScript?

Create Read More / Read Less [Functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) for [Blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) [Posts](https://www.mindstick.com/news/1939/as-market-for-digital-collectibles-tanks-meta-linked-instagram-s-nft-posts-to-facebook) in JavaScript.

## Replies

### Reply by Aryan Kumar

To create a "Read More / Read Less" functionality for blog posts in JavaScript, you can use a combination of HTML, CSS, and JavaScript. Here's a simple example using the concept of toggling between two states for a blog post. The code assumes that your blog post content is initially truncated, and clicking "Read More" expands the content, while "Read Less" collapses it.

## HTML:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <title>Read More / Read Less</title>
</head>
<body>

<div class="blog-post">
  <p class="content">
    This is the truncated content of your blog post. It is initially displayed with a "Read More" link.
  </p>
  <a href="#" class="read-more-link" onclick="toggleReadMore()">Read More</a>
</div>

<script src="script.js"></script>
</body>
</html>
```

## CSS (styles.css):

```plaintext
.blog-post {
  max-width: 400px; /* Adjust the maximum width as needed */
  margin: 0 auto;
  overflow: hidden;
}

.content {
  height: 80px; /* Adjust the initial height to show the truncated content */
  overflow: hidden;
}

.read-more-link {
  display: block;
  margin-top: 10px;
  color: blue;
  cursor: pointer;
}
```

## JavaScript (script.js):

```plaintext
function toggleReadMore() {
  var content = document.querySelector('.content');
  var readMoreLink = document.querySelector('.read-more-link');

  if (content.classList.contains('expanded')) {
    // Contract the content
    content.classList.remove('expanded');
    content.style.height = '80px'; // Adjust the initial height
    readMoreLink.textContent = 'Read More';
  } else {
    // Expand the content
    content.classList.add('expanded');
    content.style.height = content.scrollHeight + 'px';
    readMoreLink.textContent = 'Read Less';
  }
}
```

This example assumes that the content of your blog post is initially truncated with a maximum height. The JavaScript function **toggleReadMore()** is responsible for expanding and collapsing the content by adjusting the height. The CSS styles control the appearance and initial state of the blog post. Adjust the values in the CSS and JavaScript code based on your specific requirements.


---

Original Source: https://www.mindstick.com/forum/156449/create-read-more-read-less-functionality-for-blog-posts-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
