Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Collapse

Bootstrap Collapse

Description

In this tutorial, you will learn how to create collapsible components with Bootstrap.

What is required

You must reference jquery.js and bootstrap-collapse.js - both JavaScript files are located in the docs/assets/js folder.

You can create collapsible components without writing a lot of JavaScript or calling JavaScript at all.

Example

The first example demonstrates how to create a collapsible component without calling JavaScript.

Example

    

        

            

            

                

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

                

            

        

        

            

            

                

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

                

            

        

        

            

            

                

                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

                

            

        

    

View the example online

Explanation

There are three points to note here. First, add data-toggle="collapse" to the link you want to click, to expand or collapse the component.

Second, add a href or a data-target attribute to the parent component; its value is the id of the child component.

Third, add a data-parent attribute to create an accordion-style effect. The value of the data-parent attribute is the same as the value of the id attribute of the main container div (which holds the entire accordion component). If you want to create a simple collapsible component that is not as complex as an accordion, you do not need to add this attribute.

Example

The second example demonstrates how to create a simple collapsible component.

Example

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehe.

View the example online

Calling via JavaScript

You can use the following code to trigger collapsing via JavaScript.

 $(".collapse").collapse() 

Options, methods, and events

Here are some options, methods, and events used by the Bootstrap Collapsible JavaScript plugin. The details are as follows:

Option

**parent:** The value type is Selector. The default value is false. When the parent element is shown, all collapsible elements under the parent are closed.

** toggle:** The value type is Boolean. The default value is true. When called, toggles all collapsible elements.

**toggle:** The value type is Boolean. The default value is true. When called, toggles all collapsible elements.

Method

**.collapse(options):** Triggers collapsible content. Accepts an optional option object.

**.collapse('toggle'):** Shows or hides a collapsible page element.

**.collapse('show'):** Shows a collapsible page element.

**.collapse(hide):** Hides a collapsible page element.

Events

**show:** When the show instance method is called, this event is fired immediately.

**shown:** This event is fired after a collapsible page element is shown (and the CSS transition has completed).

**hide:** When the hide instance method is called, this event is fired immediately.

**hidden:** This event is fired after a collapsible page element is hidden from the user (and the CSS transition has completed).

Click here to download all the HTML, CSS, JS, and image files used in this tutorial.