Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
           CodeHim.com

Free Web Design Code & Scripts

       
  • Home
  • Code Snippets
    • Accordion
    • Creative
    • Carousel
    • Date & Time
    • Gallery
    • LightBox
    • Menu & Nav
    • Modal
    • Others
    • Portfolio
    • Social Media
    • Text & Input
    • Video Player
    • Zoom
  • HTML & CSS
  • Bootstrap
  • JavaScript
  • Collections
  • About
  • Contact
  • Buy Me a Coffee

Home / Vanilla JavaScript / Read More and Read Less using JavaScript

Read More and Read Less using JavaScript

January 22, 2024January 21, 2024 by Asif Mughal
Read More and Read Less using JavaScript
Share This:
Code Snippet:Read More, Read Less Button
Author: nl03
Published: January 21, 2024
Last Updated: January 22, 2024
Downloads: 1,882
License: MIT
Edit Code online: View on CodePen
Read More
 Demo
Download (File path is not allowed!)

This code enables you to implement “Read More” and “Read Less” functionality in your webpage using JavaScript. It hides and reveals text content to provide a concise view initially and a detailed view upon user interaction. It helps improve user experience when displaying lengthy content.

Furthermore, you can use this code in your website or web application to make long text content more user-friendly. It allows users to see a brief summary initially, reducing clutter and improving UX. When they want more details, they can click “Read more,” enhancing the overall user experience.

How to Create Read More And Read Less Using Javascript

1. In your HTML file, create a section or container where you want to apply the “Read More” functionality. Place the content you want to toggle between “Read more” and “Read less” within paragraph tags (<p>). Additionally, include a button with a unique ID, like “toggleReadMore,” which users will click to trigger the toggle.

<section>
    <p>In a shocking finding, scientists discovered a herd of unicorns living in a remote, previously unexplored valley in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.
        <span id="dots">...</span>
    </p>
    <p id="more" class="hidden">One female unicorn was especially friendly, and the researchers were able to communicate with the animals. They even named her. Scientists recently discovered the first recorded herd of wild, free-ranging unicorns in Ecuador. The researchers were amazed to see the beautiful creatures, and it was especially surprising that the animals were friendly.

        Since their discovery, the unicorns have begun to slowly be released back into the wild, though one female is still being kept as a research subject.</p>
    <button id="toggleReadMore">Read more</button>
</section>

2. In your CSS file, apply styles to the HTML elements for better presentation. You can customize the styles according to your website’s design, but ensure that the “hidden” class initially hides the additional content.

body {
    font-family: system-ui, -apple-system, sans-serif;
}

.hidden {
    display: none;
}

p {
    width: 400px;
    margin: 0;
    padding: 0;
    display: block;
}

html {
    height: 100%;
}

/* button styles, not necessary */
button {
    border: 0;
    background: #2266ff;
    color: white;
    padding: 9px 15px;
    border-radius: 7px;
    transition: 0.2s;
    display: block;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

button:hover {
    scale: 1.01;
    opacity: 0.9;
    box-shadow: 0 3px 5px rgba(0, 0, 0, 0.2);
}

button:active {
    scale: 0.99;
    box-shadow: 0 0 0 transparent;
}

3. In your JavaScript file, define the logic to toggle the visibility of the “Read more” and “Read less” content when the button is clicked. This code uses event listeners to track the button click and toggles the “hidden” class to control visibility.

let toggle = document.querySelector("#toggleReadMore"),
    more = document.querySelector("#more"),
    dots = document.querySelector("#dots"),
    isExpanded = false;

toggle.onclick = toggleReadMore;

function toggleReadMore() {
    more.classList.toggle("hidden");
    dots.classList.toggle("hidden");
    toggle.innerText = isExpanded ? "Read less" : "Read more"
    isExpanded = !isExpanded;
}

That’s it! You’ve successfully implemented “Read More” and “Read Less” functionality on your website. This user-friendly feature improves content readability and enhances the overall user experience. If you have any questions or suggestions, feel free to comment below.

Similar Code Snippets:

  • JavaScript Check if Image is Portrait or Landscape

    JavaScript Check if Image is Portrait or Landscape

  • Draggable Table Rows In JavaScript

    Draggable Table Rows In JavaScript

  • Cookie Consent Popup in JavaScript

    Cookie Consent Popup in JavaScript

  • JavaScript Piano Keyboard App

    JavaScript Piano Keyboard App

  • 3D carousel slider JavaScript

    3D carousel slider JavaScript

  • JavaScript Move Focus with Arrow Keys

    JavaScript Move Focus with Arrow Keys

  • Img Comparison Slider using Vanilla JS

    Img Comparison Slider using Vanilla JS

  • Elastic Custom Cursor in Vanilla JavaScript

    Elastic Custom Cursor in Vanilla JavaScript

  • JavaScript Generate Random Background Color

    JavaScript Generate Random Background Color

Asif Mughal

I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.

Categories Vanilla JavaScript
Breakout Game with Vanilla JavaScript
Mask Image and Text Using CSS Clip Path and SVG

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Categories

    • Accordion 22
    • Bootstrap 81
      • Bootstrap Accordion 1
      • Bootstrap Buttons 4
      • Bootstrap Cards 2
      • Bootstrap Carousel 2
      • Bootstrap Dropdowns 2
      • Bootstrap Forms 6
      • Bootstrap Jumbotron 1
      • Bootstrap Modals 2
      • Bootstrap Navbars 6
      • Bootstrap Panels 1
      • Bootstrap Progress Bars 1
      • Bootstrap Text & Input 3
    • Carousel 58
    • Chart & Graph 15
    • Date & Time 49
    • Gallery 39
    • HTML5 & CSS3 201
    • Layout 17
    • LightBox 19
    • Menu & Nav 95
    • Modal 18
    • Others 58
    • Portfolio 8
    • Social Media 10
    • Text & Input 120
    • Vanilla JavaScript 163
    • Zoom 12

You May Like

Alert (7) Analog Clock (4) Analog Clocks (5) Back to Top (4) Calculator (16) Calendar (5) Chart (3) Color Picker (4) Contact Us Forms (5) Countdown Timer (6) Datepicker (9) Digital Clocks (10) Drag and Drop (4) Dropdown Menu (23) File Uploader (4) Hamburger Menu (9) Horizontal Menu (24) Hover Effects (5) HTML5 Canvas (8) Image Sliders (40) Mega Menu (11) Modal (5) Multi-Level Menu (9) News Ticker (4) Notification (9) Off-Canvas Menu (10) Pagination (5) Parallax (8) Popup Lightbox (20) Progress Bar (5) Range Sliders (4) Scrolling Effects (28) Search Box (4) Select Dropdown (4) Shopping Cart (3) Side Menu (16) Slideshow (19) Social Share Buttons (3) Tabs (9) Text (11) Timeline (5) Timer (4) Tooltip (4) Typing Effect (6) Vertical Menu (14)

Latest Code Snippets

  • Common Open Source Security Vulnerabilities and How to Mitigate ThemCommon Open Source Security Vulnerabilities and How to Mitigate Them
    October 12, 2024
  • Animating Grid-column on Hover Using CSSAnimating Grid-column on Hover Using CSS
    April 1, 2024
  • Star Trails Animation On Mousemove in JavaScriptStar Trails Animation On Mousemove in JavaScript
    March 31, 2024
  • Gravity Button Using Pure CSSGravity Button Using Pure CSS
    March 31, 2024
  • Padlock Toggle Switch in CSSPadlock Toggle Switch in CSS
    March 31, 2024
  • Scroll-driven Animations in Pure CSSScroll-driven Animations in Pure CSS
    March 31, 2024
  • Interactive Notification Center UI in Vanilla JSInteractive Notification Center UI in Vanilla JS
    March 31, 2024
  • Emoji Spinner in Vanilla JavaScriptEmoji Spinner in Vanilla JavaScript
    March 31, 2024
  • Reveal Secret Code Using CSSReveal Secret Code Using CSS
    March 31, 2024
  • Toggle Button in CSS With Stretchable Elastic EffectToggle Button in CSS With Stretchable Elastic Effect
    March 31, 2024

Popular

  • 65+ Login Page in HTML with CSS Code Sample Simple to Difficult - 947,396 views
  • 25+ Best JavaScript Shopping Cart Examples with Demo - 211,933 views
  • Bootstrap Multiselect Dropdown with Checkboxes - 150,948 views
  • Bootstrap 5 Buttons with Icon and Text Tutorial & Demo - 112,357 views
  • 19+ Bootstrap Select Dropdown with Search Box Tutorial & Examples - 101,034 views
  • Bootstrap 5 Sidebar Menu with Submenu Collapse/Hover Tutorial Demo - 88,103 views
  • 99+ Social Media Buttons HTML Code Sample & Tutorial - 86,392 views
  • 19+ Bootstrap 5 Mega Menu Responsive/Drop Down Examples - 86,388 views
  • Bootstrap 4 Modal Popup Login Form Tutorial & Demo - 82,145 views
  • Bootstrap Vertical Menu with Submenu on Click - 80,199 views

Advertisement

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

CodeHim
  • About
  • Contact Us
  • Terms and Conditions
  • Privacy Policy
Top Categories
  • Bootstrap Snippets
  • JavaScript Snippets
  • HTML CSS Snippets
  • Menu & Nav Snippets
Get in Touch

Follow us on social media to be updated with latest web design code & scripts...

All Rights Reserved © 2026 - CodeHim Inc

Please Rel0ad/PressF5 this page if you can't click the download/preview link

F5
X