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:
<!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):
.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):
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
CSS (styles.css):
JavaScript (script.js):
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.