skip to Main Content

When clicked on first Read more button it works but Read less does not show up, however even on clicking rest Read more still the first one works.

function myFunction(id) {
  alert(id);
  var dots = document.getElementById("dots");
  var moreText = document.getElementById("more");
  if (dots.style.display === "none") {
    dots.style.display = "inline";
    id.innerHTML = "Read more";
    moreText.style.display = "none";
  } else {
    dots.style.display = "none";
    id.innerHTML = "Read less";
    moreText.style.display = "inline";
  }
}
<!-- Services Section -->
<div id="services" class="section page-split">
  <div class="section-wrapper block content-1170 center-relative">
    <div class="bg-holder float-left">
      <img src="images/left_obj_01.png" alt="">
    </div>
    <div class="section-title-holder float-left" data-background="#a13dd7">
      <div class="section-num">
        <span class="current-section-num">
          01
        </span>
        <span class="total-section-num">
          / 04
        </span>
      </div>
      <h2 class="entry-title">
        Services
      </h2>
      <p class="page-desc">
        Permanence of the stars hundreds of thousands
      </p>
    </div>
    </p>
  </div>
  <div class="section-content-holder float-right" style="margin-bottom: 30px;">
    <div class="content-wrapper">
      <div class="one_third animate wait-07s">
        <div class="service-item">
          <img class="service-icon" src="images/webdesign.png" alt="">
          <p class="service-title"><strong>Web Designing Services</strong></p>
          <p>
            Our Web Design Team consists of Professionals who have been designing websites for decades. With a wealth of experience and knowledge, we aim to provide.our clients with engaging interactive .
            <span id="dots">...</span>
            <span id="more">
              lightning .and speed websites.Our Web Design Services in Portland, USA will surely improve your website ranking as well as traffic.Contact us today for a free consultation.
            </span>
          </p>
          <button onclick="myFunction(this.id);" id="myBtn1" class="btn">Read More</button>
        </div>
      </div>
    </div>
    <div class="one_third animate wait-05s">
      <div class="service-item">
        <img class="service-icon" src="images/webdev.png" alt="">
        <p class="service-title"><strong>Web Development Services</strong></p>
        <p>
          Searching for quality Web Development Services in Portland, USA? If yes, then are experts are here to offer you the best web development services.
          <span id="dots">...</span>
          <span id="more">
            Our team consists of skilled and experienced full stack designers and developers. We have the expertise to provide high-end web solutions. Each and every website developed by us is tailored as per the requirements of your business and also as per the requirements in the market.
          </span>
          </span>
        </p>
        <button onclick="myFunction(this.id);" id="myBtn2" class="btn">Read More</button>
      </div>
    </div>
    <div class="one_third last animate wait-04s">
      <div class="service-item">
        <img class="service-icon" src="images/ecomm.png" alt="">
        <p class="service-title"><strong>E-commerce Website Development</strong></p>
        <p>
          Codevelop has been operating in the digital marketing sector for several years.We have been fortunate enough to have worked with several clients..
          <span id="dots">...</span>
          <span id="more">
            from all around the world who are now successfully commencing business in the Ecommerce world..The primary objective of our company is to meet all your Ecommerce website development requirements so that you can attract more customers and simultaneously maximize your sales in the long run.Codevelop is the best Ecommerce website developer in all of Portland, USA as our company now aims to give you the best possible digital marketing solutions at affordable pricing points.
            </span>
          </span>
        </p>
        <button onclick="myFunction(this.id);" id="myBtn3" class="btn">Read More</button>
      </div>
    </div>
    <div class="clear"></div>
    <div class="one_third animate wait-07s">
      <div class="service-item">
        <img class="service-icon" src="images/file.png" alt="">
        <p class="service-title"><strong>Graphic Designing</strong></p>
        <p>Build a stronger brand with the help of our Graphic Designing Services
          <span class="para4" style="display:none;">
            With the ever-growing demand in the digital world, the level of competition is also increasing at
a rate of knots. Therefore, companies must rely on impressive graphic designs in order to
attract the attention of the target audience. Avail the services of Codevelop today and build a
competitive advantage in favor of your brand within your target market.
Our team of experts possesses the skills to meet all your requirements. Complete customer
satisfaction is the sole objective of our company and we aim to achieve it at any cost.
          </span>
          </span>
        </p>
        <button onclick="myFunction(this.id);" id="myBtn4" class="btn">Read More</button>
      </div>
    </div>
    <div class="one_third animate wait-05s">
      <div class="service-item">
        <img class="service-icon" src="images/seo.png" alt="">
        <p class="service-title"><strong>SEO Services</strong></p>
        <p>
          Dominate all Search Engine Platforms by joining hands with Codevelop. Search
          <span class="para5" style="display:none;">Engine Optimization or SEO is perhaps the most powerful tool that exists in the digital
world today. If SEO strategies are correctly applied,
          </span>
          </span>
        </p>
        <button onclick="myFunction(this.id);" id="myBtn5" class="btn">Read More</button>
      </div>
    </div>
    <div class="one_third last animate wait-04s">
      <div class="service-item">
        <img class="service-icon" src="images/wordpress.png" alt="">
        <p class="service-title"><strong>Word Press Services</strong></p>
        <p>
          Want more stability for your business? Hire the experts at Codevelop. In order to
          <span class="para6" style="display:none;">
            be relevant in the present market, it is essential that you have a website that is user-friendly, easily accessible, and can be adequately navigated. All these aspects and more can be achieved with the help of our services. Here at Codevelop, we possess a dedicated team of individuals who specialize in various fields of study. This is precisely why we are able to cater to the various digital marketing needs of our customers.
          </span>
          </span>
        </p>
        <button onclick="myFunction(this.id);" id="myBtn6" class="btn">Read more</button>
      </div>
    </div>
  </div>
</div>

2

Answers


  1. As your code is incomplete but i think you want to do that

    HTML

        <div class="well">
        <h3><a href="#">Market Research, Analysis, and Strategy Consultant</a></h3>
    
        <!-- Replace the 'height' below as you want -->
        <p class="lead" height="36">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam purus, lacinia eget placerat sit amet, bibendum nec nisl. Curabitur a mattis ipsum. Praesent quis nisi in purus malesuada rhoncus. Pellentesque ut quam eget libero congue lobortis. Nunc sed quam ac erat lobortis eleifend. Donec elementum sodales cursus. Aliquam porttitor massa nisi, in laoreet turpis. Sed consequat condimentum lorem ut dignissim. Sed hendrerit mauris ut massa fermentum laoreet. Pellentesque a leo vitae enim dictum lobortis. Praesent commodo feugiat velit iaculis malesuada.</p>
    
        <div class="more more-lead">More...</div>
    </div>
    <div class="well">
        <h3><a href="#">Market Research, Analysis, and Strategy Consultant 2</a></h3>
    
        <!-- Replace the 'height' below as you want -->
        <p class="lead" height="36">2 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam purus, lacinia eget placerat sit amet, bibendum nec nisl. Curabitur a mattis ipsum. Praesent quis nisi in purus malesuada rhoncus. Pellentesque ut quam eget libero congue lobortis. Nunc sed quam ac erat lobortis eleifend. Donec elementum sodales cursus. Aliquam porttitor massa nisi, in laoreet turpis. Sed consequat condimentum lorem ut dignissim. Sed hendrerit mauris ut massa fermentum laoreet. Pellentesque a leo vitae enim dictum lobortis. Praesent commodo feugiat velit iaculis malesuada.</p>
    
        <div class="more more-lead">More...</div>
    </div>
    

    CSS

    .lead {float:left; width:100%; overflow:hidden; background-color:#66FF00;}
    .more {cursor:pointer;}
    

    JS

     jQuery(function($){
    
        // Resize Height
        function reh(e) {
            $(e).css({'height':$(e).attr('height')})
        }
    
        // Empty Height
        function emh(e) {
            $(e).css({'height':'auto'});
        }
    
        // Resize .lead (default height)
        reh('.lead');
    
        // Actions
        // More: Lead
        $('body').on('click', '.more-lead', function(){
            emh($(this).siblings('.lead'));
            $(this).html('Less...').addClass('less-lead').removeClass('more-lead');
        });
    
        // Less: Lead
        $('body').on('click', '.less-lead', function(){
            reh($(this).siblings('.lead'));
            $(this).html('More...').addClass('more-lead').removeClass('less-lead');
        });
    
    });
    
    Login or Signup to reply.
  2. Before telling you what is the issue, let me give you some tips that in my opinion will help you with your code:

    1. There are extra HTML tags:

    Proper indentation and formatting of the code will help you to avoid mistakes like this one:

    <div class="section-wrapper block content-1170 center-relative">
        <div class="bg-holder float-left">
            <img src="images/left_obj_01.png" alt="">
        </div>
        <div class="section-title-holder float-left" data-background="#a13dd7">
            <div class="section-num">
                <span class="current-section-num">
                    01
                </span>
                <span class="total-section-num">
                    / 04
                </span>
            </div>
            <h2 class="entry-title">
                Services
            </h2>
            <p class="page-desc">
                Permanence of the stars  hundreds of thousands
            </p>
        </div>
        </p> <------ Extra p tag
    </div>
    

    2. Avoid using JavaScript in HTML properties:

    It will add another layer of complexity and your code will be more error prone, because if you delete a function, you need to remove it from the HTML code and linters will not warn you about this.

    3. Instead adding display properties with JavaScript it is better to work with classes and add or remove them:

    If you don’t need to support Internet Explorer 9, you can use Element.classList property to add or remove a class from the elements. In the final snippet you’ll see an example of its use.

    4. Don’t add the same id to multiple elements:

    Ids are intended to be unique in your page, if you need to access to multiple elements, use a class instead.

    5. And your main issue is this one:

    As ids should be unique in the page, document.getElementById will return the first element in the DOM with that id. So your function is trying to show and hide only the first elements in the document with the ids dots and more.

    Here you have your code using the previous recommendations:

    var services = document.querySelector('#services');
    
    services.addEventListener('click', function(e) {
      var target = e.target;
      if (target.classList.contains('btn')) {
        var serviceItem = target.parentNode;
        var open = target.dataset.open === 'true';
        target.dataset.open = !open;
        if (open) {
          target.innerText = 'Read more';
          serviceItem.classList.remove('open');
        } else {
          target.innerText = 'Read less';
          serviceItem.classList.add('open');
        }
      }
    });
    .more {
      display: none;
    }
    
    .service-item.open .more {
      display: inline;
    }
    
    .service-item.open .dots {
      display: none;
    }
    <!-- Services Section -->
    <div id="services" class="section page-split">
      <div class="section-wrapper block content-1170 center-relative">
        <div class="bg-holder float-left">
          <img src="images/left_obj_01.png" alt="">
        </div>
        <div class="section-title-holder float-left" data-background="#a13dd7">
          <div class="section-num">
            <span class="current-section-num">
              01
            </span>
            <span class="total-section-num">
              / 04
            </span>
          </div>
          <h2 class="entry-title">
            Services
          </h2>
          <p class="page-desc">
            Permanence of the stars hundreds of thousands
          </p>
        </div>
      </div>
      <div class="section-content-holder">
        <div class="content-wrapper">
          <div class="one_third animate wait-07s">
            <div class="service-item">
              <img class="service-icon" src="images/webdesign.png" alt="">
              <p class="service-title"><strong>Web Designing Services</strong></p>
              <p>Our Web Design Team consists of Professionals who have been designing websites for decades. With a wealth of experience and knowledge, we aim to provide.our clients with engaging interactive .<span class="dots">...</span><span class="more">lightning .and speed websites.Our Web Design Services in Portland, USA will surely improve your website ranking as well as traffic.Contact us today for a free consultation.</span>
              </p>
              <button id="myBtn1" class="btn">Read More</button>
            </div>
          </div>
        </div>
        <div class="one_third animate wait-05s">
          <div class="service-item">
            <img class="service-icon" src="images/webdev.png" alt="">
            <p class="service-title"><strong>Web Development Services</strong></p>
            <p>Searching for quality Web Development Services in Portland, USA? If yes, then are experts are here to offer you the best web development services.<span class="dots">...</span><span class="more">Our team consists of skilled and experienced full stack designers and developers. We have the expertise to provide high-end web solutions. Each and every website developed by us is tailored as per the requirements of your business and also as per the requirements in the market.</span>
            </p>
            <button id="myBtn2" class="btn">Read More</button>
          </div>
        </div>
        <div class="one_third last animate wait-04s">
          <div class="service-item">
            <img class="service-icon" src="images/ecomm.png" alt="">
            <p class="service-title"><strong>E-commerce Website Development</strong></p>
            <p>Codevelop has been operating in the digital marketing sector for several years.We have been fortunate enough to have worked with several clients..<span class="dots">...</span><span class="more">from all around the world who are now successfully commencing business in the Ecommerce world..The primary objective of our company is to meet all your Ecommerce website development requirements so that you can attract more customers and simultaneously maximize your sales in the long run.Codevelop is the best Ecommerce website developer in all of Portland, USA as our company now aims to give you the best possible digital marketing solutions at affordable pricing points.</span>
            </p>
            <button id="myBtn3" class="btn">Read More</button>
          </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search