skip to Main Content

I wrote a code javascript code that will display the complete write up of a particular text which happens to be in three location in my html, but instead of displaying just the particular one that was clicked, its displaying others along codepen side. code screen shot

i was trying to make an hidden text non hidden upon clicking of the read more with bold print for individaul read me. check right side of my screen shot for the result

const readMore = document.querySelectorAll('.readMore')
const contents = document.querySelectorAll('.moreRead')

// const content = document.querySelector('.moreRead')


readMore.forEach((read, index) => {
  
  read.addEventListener('click', (e) => {
    console.log(e);
    console.log(index);
    read.classList.add('d-none');


    // content.classList.remove('d-none')

    contents.forEach(content =>{
      content.classList.remove('d-none')
    })
    

  })

})

2

Answers


  1. The issue is because when any one of .readMore are clicked you loop over every .moreRead and display them – not only the one which is related to the clicked element.

    To fix this, you can use parentElement and querySelector() to target the sibling of the clicked .readMore and updated its class:

    const readMore = document.querySelectorAll('.readMore');
    
    readMore.forEach((read, index) => {
      read.addEventListener('click', (e) => {
        read.classList.add('d-none');
        read.parentElement.querySelector('.moreRead').classList.remove('d-none');
      })
    })
    .d-none {
      display: none;
    }
    <div class="card mt-5 border-0" style="background-color: #eee984;">
      <img class="card-img-top img-fluid" src="/assets/card-one-image.jpg" alt="Title">
      <div class="card-body">
        <h5 class="card-title">Business Development</h5>
        <p class="card-text fw-bold fs-5">The new way to fast track business operations isnot somthing</p>
        <p class="card-text">
          The new way to fast track business operations in an economic crises is not something
          <span class="readMore fw-bold" style="cursor: pointer;">....Read More</span>
          <span class="moreRead d-none">
            card 1 ipsum dolor sit amet consectetur
            adipisicing elit. Laborum, cupiditate aspernatur nam voluptate deleniti explicabo
            totam rem. Accusantium molestiae vero voluptatum a, fuga repudiandae, rem saepe
            nobis maiores quisquam eligendi.
            Animi inventore exercitationem soluta magni iure, odit rem sed, porro dolore ut
            totam itaque. Eum nemo sint fugiat tempore saepe inventore rerum, quis minus vel
            sunt voluptate doloremque dignissimos cum.
          </span>
        </p>
      </div>
      <div class="d-flex">
        <div><img style="width: 40px; height: 40px; border-radius: 100%;" class="img-fluid mt-3 me-2" src="/assets/alexander-krivitskiy-TD9rSZywT7Y-unsplash.jpg" alt=""></div>
        <div>
          <div class="fw-bold mt-2">joanne david</div>
          <p>11 jan 2022 -5mins read</p>
        </div>
      </div>
    </div>
    
    <div class="card mt-5 border-0" style="background-color: #eee984;">
      <img class="card-img-top img-fluid" src="/assets/card-one-image.jpg" alt="Title">
      <div class="card-body">
        <h5 class="card-title">Business Development</h5>
        <p class="card-text fw-bold fs-5">The new way to fast track business operations isnot somthing</p>
        <p class="card-text">
          The new way to fast track business operations in an economic crises is not something
          <span class="readMore fw-bold" style="cursor: pointer;">....Read More</span>
          <span class="moreRead d-none">
            card 1 ipsum dolor sit amet consectetur
            adipisicing elit. Laborum, cupiditate aspernatur nam voluptate deleniti explicabo
            totam rem. Accusantium molestiae vero voluptatum a, fuga repudiandae, rem saepe
            nobis maiores quisquam eligendi.
            Animi inventore exercitationem soluta magni iure, odit rem sed, porro dolore ut
            totam itaque. Eum nemo sint fugiat tempore saepe inventore rerum, quis minus vel
            sunt voluptate doloremque dignissimos cum.
          </span>
        </p>
      </div>
      <div class="d-flex">
        <div><img style="width: 40px; height: 40px; border-radius: 100%;" class="img-fluid mt-3 me-2" src="/assets/alexander-krivitskiy-TD9rSZywT7Y-unsplash.jpg" alt=""></div>
        <div>
          <div class="fw-bold mt-2">joanne david</div>
          <p>11 jan 2022 -5mins read</p>
        </div>
      </div>
    </div>
    Login or Signup to reply.
  2. What I have understood about what you’re trying to do;
    You have three sections each with a read more button to toggle that section’s visibility on or off.

    However in your toggle buttons click event listeners, you turn on the visibility of all the collapsible sections by removing the d-none class. What you should instead do is remove the d-none class from only the section the toggle button controls.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search