Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read more/less jQuery accordion

I'm searching for a jQuery accordion like the one that can be found on the avg website with no luck, so I'm hoping someone here can point me into the right direction. To source code or an example.

I already have the accordion working but it's the read more/close feature that is missing and that I want to use.

like image 907
robonhigh Avatar asked Oct 11 '13 18:10

robonhigh


People also ask

How do you create read more read less content using jQuery?

Create an HTML Div with Some Texts It is optional. You may skip this step. Create another child div with class="invisible-content" to display whole text after clicking read more. After that, create an HTML button with class="btn more-less" to read more or less content on the click event.

How do I hide show more text in a certain length?

Answer: Use the JavaScript substring() method You can use the JavaScript substring() method in combination with the jQuery append() and html() methods to truncate the paragraphs of a text and add read more link at the end, if the number of characters inside a paragraph exceeds a certain length.

How to add Read More using jQuery?

Read more functionality is used in jQuery to load more data when the user clicks on the read more button. JQuery was introduced to simplify HTML DOM tree traversal and manipulation and event handling, CSS animation, and Ajax.

How do I add a Read More link at the end of a paragraph?

html(short_content+ '<a href="#" class="read_more"><br/>read more... </a>'+ '<span class="more_text" style="display:none;">'+long_content+'</span>'); /* Alter the html to allow the read more functionality */ $(this).


2 Answers

const moreText = "Read more";
const lessText = "Read less";
const moreButton = $("button.readmorebtn");

moreButton.click(function() {
  const $this = $(this);
  $this.text($this.text() == moreText ? lessText : moreText).next(".more").slideToggle("fast");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.
  </p>
  <button class="readmorebtn" type="button">Read more</button>
  <p class="more" style="display:none">Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Curabitur blandit tempus porttitor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus mollis interdum. Etiam porta sem malesuada magna mollis euismod.
    Praesent commodo cursus magna, vel scelerisque nisl consectetur.
  </p>
</div>
like image 151
Bram Vanroy Avatar answered Oct 18 '22 15:10

Bram Vanroy


Jquery UI is great, but its a lot of extra stuff just for one little accordion. If you are going to use all of the features, then go for it.

You could also just use the jQuery methods show(), hide() or toggle(). Follow the links to learn about those, I won't explain them here.

The problem with all of those is they show or hide ALL of the text, as some of the other answers here demonstrate. If you want to show a line or two (or ten) of the actual text, then display all of it by clicking a button/link, you need a little more than the canned methods. If you just want a little script to do that, or just want to understand how its done, here is my little plugin. My script animates the open and closing of the accordion similar to show()/hide(), but will show a pre-defined amount of text in the closed position.

Fiddle

Hope this helps

HTML

<div class="show-more-snippet">
//your text goes here//
</div>
<a href="#" class="show-more">More...</a>

CSS

.show-more-snippet {
    height:35px;  /*this is set to the height of the how much text you want to show based on the font size, line height, etc*/
    width:300px;
    overflow:hidden;
}

jQuery

$('.show-more').click(function() {
    if($('.show-more-snippet').css('height') != '35px'){
        $('.show-more-snippet').stop().animate({height: '35px'}, 200);
        $(this).text('More...');
    }else{
        $('.show-more-snippet').css({height:'100%'});
        var xx = $('.show-more-snippet').height();
        $('.show-more-snippet').css({height:'35px'});
        $('.show-more-snippet').stop().animate({height: xx}, 400);
        // ^^ The above is beacuse you can't animate css to 100%.  So I change it to 100%, get the value, change it back, then animate it to the value. If you don't want animation, you can ditch all of it and just leave: $('.show-more-snippet').css({height:'100%'});^^ //
        $(this).text('Less...');
    }

});

like image 41
sn3ll Avatar answered Oct 18 '22 13:10

sn3ll