最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Read more & Read less in website page content using Javascript - Stack Overflow

programmeradmin5浏览0评论

Expected output: i want Read more & Read less click option be at the bottom with all content which include paragraph & list.. (Codepen shows Read more & Read less click option at the top) Here what i tried: HTML

<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
<div class="content">Testing
  <ul>
    <li>first</li>
  </ul>
</div>

Javascript:

$(document).ready(function () {
  $(".content").hide();
  $(".show_hide").on("click", function () {
    var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
    $(".show_hide").text(txt);
    $(this).next('.content').slideToggle(200);
  });
});

Codepen link for Read more & read less implementation: enter link description here

Expected output: i want Read more & Read less click option be at the bottom with all content which include paragraph & list.. (Codepen shows Read more & Read less click option at the top) Here what i tried: HTML

<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
<div class="content">Testing
  <ul>
    <li>first</li>
  </ul>
</div>

Javascript:

$(document).ready(function () {
  $(".content").hide();
  $(".show_hide").on("click", function () {
    var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
    $(".show_hide").text(txt);
    $(this).next('.content').slideToggle(200);
  });
});

Codepen link for Read more & read less implementation: enter link description here

Share Improve this question edited Jun 25, 2020 at 14:04 zab asked Jun 23, 2020 at 4:19 zabzab 952 gold badges2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

To move Read More link to the bottom and have it work use "prev" instead of "next" method.

        $(document).ready(function () {
    $(".content").hide();
    $(".show_hide").on("click", function () {
        var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
        $(".show_hide").text(txt);
        $(this).prev('.content').slideToggle(200);
    });
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">Testing
<ul>
<li>first</li>
</ul>

</div>
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>

Try this :

$(document).ready(function () {
  $(".show_hide").on("click", function () {     
    var txt = $(".content").hasClass('visible') ? 'Read More' : 'Read Less';
    $(".show_hide").text(txt);
    $(".content").toggleClass("visible");
  });
});
.content {
  height:90px;
  overflow:hidden;
  margin-bottom:10px;
}

.content.visible {
  height:auto;
  overflow:visible;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">Testing
  <ul>
    <li>first</li>
    <li>first</li>
    <li>first</li>
    <li>first</li>
    <li>first</li>
    <li>first</li>
    <li>first</li>
    <li>first</li>
  </ul>  
</div>
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>

You can use css for this

.show_hide{
  position:absolute;
  top:100%;
}

Or just put it at the bottom of a container that is what people do

Your content class (which gets shown and hidden) contains all of your visible content. You just need to include some content that does not have the content class before your a element, something like:

$(document).ready(function() {
  $(".content").hide();
  $(".show_hide").on("click", function() {
    var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
    $(".show_hide").text(txt);
    $(this).next('.content').slideToggle(200);
  });
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <h3>Always shown</h3>
  <p>Some text</p>
</div>
<a href="#" class="show_hide">Read More</a>
<div class="content">
  <h3>Sometimes shown</h3>
  <p>More text</p>
</div>