Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap collapse change button to read less on click

I have the following bootstrap code:

<div class="col-lg-4 col-md-4 col-sm-4" id="collapseTwo">
      <h2>What</h2> 
      <p>Find out what we do</p>
      <div id="what" class="collapse">
       <p>Text here.</p>

      <a class="btn btn-default" data-toggle="collapse" data-parent="#threeW" href="#what" role="button">Read More &raquo;</a>
    </div>

On click of the button, it will display the text inside "what". I want the button to change from "read more" to "Read Less" when done. Ive found other examples but they dont seem to be with the collapse function and not sure how to implement it.

Any help or pointers in the right direction would be appreciated, thanks.

like image 830
Martyn Avatar asked Feb 28 '14 08:02

Martyn


People also ask

How do I change the collapse button in bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.

How do you collapse a div on a button click?

The . collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.

What is data BS toggle?

The data-toggle is an HTML-5 data attribute defined in Bootstrap. The advantage of using this is that, you can select a class or an id and hook up the element with a particular widget.

What is Aria controls bootstrap?

The aria-controls attribute identifies the element (or elements) whose contents or presence are controlled by the element on which the attribute is set, regardless of what type of interaction initiates the impacted behavior.


1 Answers

I done this only using HTML and CSS Check out this example.

[aria-expanded="false"] > .expanded,
[aria-expanded="true"] > .collapsed {
  display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit non deleniti reiciendis expedita, velit
    excepturi doloremque numquam. Blanditiis, accusantium, at.</p>

  <p>
    <!-- aria-expanded attribute is mandatory -->
    <!-- bootstrap changes it to true/false on toggle -->
    <a href="#block-id" class="btn btn-primary" data-toggle="collapse" aria-expanded="false" aria-controls="block-id">
      <span class="collapsed">
        Show more
      </span>
      <span class="expanded">
        Show Less
      </span>
    </a>
  </p>

  <p class="collapse" id="block-id">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi tempore asperiores et vel perferendis ea
    quisquam eos incidunt veritatis, hic porro voluptates temporibus laborum! Saepe natus vitae eum ex nam, laborum
    optio vel magni hic. Minima pariatur molestiae error a quae sed accusantium voluptas, dolore, mollitia accusamus
    laborum!
  </p>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
like image 80
iamandrewluca Avatar answered Oct 15 '22 09:10

iamandrewluca