Javascript for Bootstrap

Bring Bootstrap's components to life—now with 12 custom jQuery plugins.

Modals

A streamlined, but flexible, take on the traditional javascript modal plugin with only the minimum required functionality and smart defaults.

Dropdowns

Add dropdown menus to nearly anything in Bootstrap with this simple plugin. Bootstrap features full dropdown menu support on in the navbar, tabs, and pills.

Scrollspy

Use scrollspy to automatically update the links in your navbar to show the current active link based on scroll position.

Togglable tabs

Use this plugin to make tabs and pills more useful by allowing them to toggle through tabbable panes of local content.

Tooltips

A new take on the jQuery Tipsy plugin, Tooltips don't rely on images—they use CSS3 for animations and data-attributes for local title storage.

Popovers *

Add small overlays of content, like those on the iPad, to any element for housing secondary information.

* Requires Tooltips to be included

Alert messages

The alert plugin is a tiny class for adding close functionality to alerts.

Buttons

Do more with buttons. Control button states or create groups of buttons for more components like toolbars.

Collapse

Get base styles and flexible support for collapsible components like accordions and navigation.

Carousel

Create a merry-go-round of any content you wish to provide an interactive slideshow of content.

Typeahead

A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

Transitions *

For simple transition effects, include bootstrap-transition.js once to slide in modals or fade out alerts.

* Required for animation in plugins

Heads up! All javascript plugins require the latest version of jQuery.

About modals

A streamlined, but flexible, take on the traditional javascript modal plugin with only the minimum required functionality and smart defaults.

Download file

Static example

Below is a statically rendered modal.

Live demo

Toggle a modal via javascript by clicking the button below. It will slide down and fade in from the top of the page.

Launch demo modal

Using bootstrap-modal

Call the modal via javascript:

  1. $('#myModal').modal(options)

Options

Name type default description
backdrop boolean true Includes a modal-backdrop element. Alternatively, specify static for a backdrop which doesn't close the modal on click.
keyboard boolean true Closes the modal when escape key is pressed
show boolean true Shows the modal when initialized.

Markup

You can activate modals on your page easily without having to write a single line of javascript. Just set data-toggle="modal" on a controller element with a data-target="#foo" or href="#foo" which corresponds to a modal element id, and when clicked, it will launch your modal.

Also, to add options to your modal instance, just include them as additional data attributes on either the control element or the modal markup itself.

  1. <a class="btn" data-toggle="modal" href="#myModal" >Launch Modal</a>
  1. <div class="modal hide" id="myModal">
  2. <div class="modal-header">
  3. <button type="button" class="close" data-dismiss="modal">×</button>
  4. <h3>Modal header</h3>
  5. </div>
  6. <div class="modal-body">
  7. <p>One fine body…</p>
  8. </div>
  9. <div class="modal-footer">
  10. <a href="#" class="btn" data-dismiss="modal">Close</a>
  11. <a href="#" class="btn btn-primary">Save changes</a>
  12. </div>
  13. </div>
Heads up! If you want your modal to animate in and out, just add a .fade class to the .modal element (refer to the demo to see this in action) and include bootstrap-transition.js.

Methods

.modal(options)

Activates your content as a modal. Accepts an optional options object.

  1. $('#myModal').modal({
  2. keyboard: false
  3. })

.modal('toggle')

Manually toggles a modal.

  1. $('#myModal').modal('toggle')

.modal('show')

Manually opens a modal.

  1. $('#myModal').modal('show')

.modal('hide')

Manually hides a modal.

  1. $('#myModal').modal('hide')

Events

Bootstrap's modal class exposes a few events for hooking into modal functionality.

Event Description
show This event fires immediately when the show instance method is called.
shown This event is fired when the modal has been made visible to the user (will wait for css transitions to complete).
hide This event is fired immediately when the hide instance method has been called.
hidden This event is fired when the modal has finished being hidden from the user (will wait for css transitions to complete).
  1. $('#myModal').on('hidden', function () {
  2. // do something…
  3. })

The ScrollSpy plugin is for automatically updating nav targets based on scroll position.

Download file

Example navbar with scrollspy

Scroll the area below and watch the navigation update. The dropdown sub items will be highlighted as well. Try it!

@fat

Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.

@mdo

Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.

one

Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.

two

In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.

three

Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.

Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.


Using bootstrap-scrollspy.js

Call the scrollspy via javascript:

  1. $('#navbar').scrollspy()

Markup

To easily add scrollspy behavior to your topbar navigation, just add data-spy="scroll" to the element you want to spy on (most typically this would be the body).

  1. <body data-spy="scroll" >...</body>
Heads up! Navbar links must have resolvable id targets. For example, a <a href="#home">home</a> must correspond to something in the dom like <div id="home"></div>.

Methods

.scrollspy('refresh')

When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method like so:

  1. $('[data-spy="scroll"]').each(function () {
  2. var $spy = $(this).scrollspy('refresh')
  3. });

Options

Name type default description
offset number 10 Pixels to offset from top when calculating position of scroll.

Events

Event Description
activate This event fires whenever a new item becomes activated by the scrollspy.

This plugin adds quick, dynamic tab and pill functionality for transitioning through local content.

Download file

Example tabs

Click the tabs below to toggle between hidden panes, even via dropdown menus.

Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.

Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.


Using bootstrap-tab.js

Enable tabbable tabs via javascript (each tab needs to be activated individually):

  1. $('#myTab a').click(function (e) {
  2. e.preventDefault();
  3. $(this).tab('show');
  4. })

You can activate individual tabs in several ways:

  1. $('#myTab a[href="#profile"]').tab('show'); // Select tab by name
  2. $('#myTab a:first').tab('show'); // Select first tab
  3. $('#myTab a:last').tab('show'); // Select last tab
  4. $('#myTab li:eq(2) a').tab('show'); // Select third tab (0-indexed)

Markup

You can activate a tab or pill navigation without writing any javascript by simply specifying data-toggle="tab" or data-toggle="pill" on an element. Adding the nav and nav-tabs classes to the tab ul will apply the bootstrap tab styling.

  1. <ul class="nav nav-tabs">
  2. <li><a href="#home" data-toggle="tab">Home</a></li>
  3. <li><a href="#profile" data-toggle="tab">Profile</a></li>
  4. <li><a href="#messages" data-toggle="tab">Messages</a></li>
  5. <li><a href="#settings" data-toggle="tab">Settings</a></li>
  6. </ul>

Methods

$().tab

Activates a tab element and content container. Tab should have either a data-target or an href targeting a container node in the DOM.

  1. <ul class="nav nav-tabs" id="myTab">
  2. <li class="active"><a href="#home">Home</a></li>
  3. <li><a href="#profile">Profile</a></li>
  4. <li><a href="#messages">Messages</a></li>
  5. <li><a href="#settings">Settings</a></li>
  6. </ul>
  7.  
  8. <div class="tab-content">
  9. <div class="tab-pane active" id="home">...</div>
  10. <div class="tab-pane" id="profile">...</div>
  11. <div class="tab-pane" id="messages">...</div>
  12. <div class="tab-pane" id="settings">...</div>
  13. </div>
  14.  
  15. <script>
  16. $(function () {
  17. $('#myTab a:last').tab('show');
  18. })
  19. </script>

Events

Event Description
show This event fires on tab show, but before the new tab has been shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab (if available) respectively.
shown This event fires on tab show after a tab has been shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab (if available) respectively.
  1. $('a[data-toggle="tab"]').on('shown', function (e) {
  2. e.target // activated tab
  3. e.relatedTarget // previous tab
  4. })

About Tooltips

Inspired by the excellent jQuery.tipsy plugin written by Jason Frame; Tooltips are an updated version, which don't rely on images, use css3 for animations, and data-attributes for local title storage.

Download file

Example use of Tooltips

Hover over the links below to see tooltips:

Tight pants next level keffiyeh you probably haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown. Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel have a terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats. Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan whatever keytar, scenester farm-to-table banksy Austin twitter handle freegan cred raw denim single-origin coffee viral.


Using bootstrap-tooltip.js

Trigger the tooltip via javascript:

  1. $('#example').tooltip(options)

Options

Name type default description
animation boolean true apply a css fade transition to the tooltip
placement string|function 'top' how to position the tooltip - top | bottom | left | right
selector string false If a selector is provided, tooltip objects will be delegated to the specified targets.
title string | function '' default title value if `title` tag isn't present
trigger string 'hover' how tooltip is triggered - hover | focus | manual
delay number | object 0

delay showing and hiding the tooltip (ms) - does not apply to manual trigger type

If a number is supplied, delay is applied to both hide/show

Object structure is: delay: { show: 500, hide: 100 }

Heads up! Options for individual tooltips can alternatively be specified through the use of data attributes.

Markup

For performance reasons, the Tooltip and Popover data-apis are opt in. If you would like to use them just specify a selector option.

  1. <a href="#" rel="tooltip" title="first tooltip">hover over me</a>

Methods

$().tooltip(options)

Attaches a tooltip handler to an element collection.

.tooltip('show')

Reveals an element's tooltip.

  1. $('#element').tooltip('show')

.tooltip('hide')

Hides an element's tooltip.

  1. $('#element').tooltip('hide')

.tooltip('toggle')

Toggles an element's tooltip.

  1. $('#element').tooltip('toggle')

About popovers

Add small overlays of content, like those on the iPad, to any element for housing secondary information.

* Requires Tooltip to be included

Download file

Example hover popover

Hover over the button to trigger the popover.


Using bootstrap-popover.js

Enable popovers via javascript:

  1. $('#example').popover(options)

Options

Name type default description
animation boolean true apply a css fade transition to the tooltip
placement string|function 'right' how to position the popover - top | bottom | left | right
selector string false if a selector is provided, tooltip objects will be delegated to the specified targets
trigger string 'hover' how tooltip is triggered - hover | focus | manual
title string | function '' default title value if `title` attribute isn't present
content string | function '' default content value if `data-content` attribute isn't present
delay number | object 0

delay showing and hiding the popover (ms) - does not apply to manual trigger type

If a number is supplied, delay is applied to both hide/show

Object structure is: delay: { show: 500, hide: 100 }

Heads up! Options for individual popovers can alternatively be specified through the use of data attributes.

Markup

For performance reasons, the Tooltip and Popover data-apis are opt in. If you would like to use them just specify a selector option.

Methods

$().popover(options)

Initializes popovers for an element collection.

.popover('show')

Reveals an elements popover.

  1. $('#element').popover('show')

.popover('hide')

Hides an elements popover.

  1. $('#element').popover('hide')

.popover('toggle')

Toggles an elements popover.

  1. $('#element').popover('toggle')

About alerts

The alert plugin is a tiny class for adding close functionality to alerts.

Download

Example alerts

The alerts plugin works on regular alert messages, and block messages.

Holy guacamole! Best check yo self, you're not looking too good.

Oh snap! You got an error!

Change this and that and try again. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.

Take this action Or do this


Using bootstrap-alert.js

Enable dismissal of an alert via javascript:

  1. $(".alert").alert()

Markup

Just add data-dismiss="alert" to your close button to automatically give an alert close functionality.

  1. <a class="close" data-dismiss="alert" href="#">&times;</a>

Methods

$().alert()

Wraps all alerts with close functionality. To have your alerts animate out when closed, make sure they have the .fade and .in class already applied to them.

.alert('close')

Closes an alert.

  1. $(".alert").alert('close')

Events

Bootstrap's alert class exposes a few events for hooking into alert functionality.

Event Description
close This event fires immediately when the close instance method is called.
closed This event is fired when the alert has been closed (will wait for css transitions to complete).
  1. $('#my-alert').bind('closed', function () {
  2. // do something…
  3. })

About

Do more with buttons. Control button states or create groups of buttons for more components like toolbars.

Download file

Example uses

Use the buttons plugin for states and toggles.

Stateful
Single toggle
Checkbox
Radio

Using bootstrap-button.js

Enable buttons via javascript:

  1. $('.nav-tabs').button()

Markup

Data attributes are integral to the button plugin. Check out the example code below for the various markup types.

  1. <!-- Add data-toggle="button" to activate toggling on a single button -->
  2. <button class="btn" data-toggle="button">Single Toggle</button>
  3.  
  4. <!-- Add data-toggle="buttons-checkbox" for checkbox style toggling on btn-group -->
  5. <div class="btn-group" data-toggle="buttons-checkbox">
  6. <button class="btn">Left</button>
  7. <button class="btn">Middle</button>
  8. <button class="btn">Right</button>
  9. </div>
  10.  
  11. <!-- Add data-toggle="buttons-radio" for radio style toggling on btn-group -->
  12. <div class="btn-group" data-toggle="buttons-radio">
  13. <button class="btn">Left</button>
  14. <button class="btn">Middle</button>
  15. <button class="btn">Right</button>
  16. </div>

Methods

$().button('toggle')

Toggles push state. Gives the button the appearance that it has been activated.

Heads up! You can enable auto toggling of a button by using the data-toggle attribute.
  1. <button class="btn" data-toggle="button" ></button>

$().button('loading')

Sets button state to loading - disables button and swaps text to loading text. Loading text should be defined on the button element using the data attribute data-loading-text.

  1. <button class="btn" data-loading-text="loading stuff..." >...</button>
Heads up! Firefox persists the disabled state across page loads. A workaround for this is to use autocomplete="off".

$().button('reset')

Resets button state - swaps text to original text.

$().button(string)

Resets button state - swaps text to any data defined text state.

  1. <button class="btn" data-complete-text="finished!" >...</button>
  2. <script>
  3. $('.btn').button('complete')
  4. </script>

About

Get base styles and flexible support for collapsible components like accordions and navigation.

Download file

* Requires the Transitions plugin to be included.

Example accordion

Using the collapse plugin, we built a simple accordion style widget:

Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

Using bootstrap-collapse.js

Enable via javascript:

  1. $(".collapse").collapse()

Options

Name type default description
parent selector false If selector then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior)
toggle boolean true Toggles the collapsible element on invocation

Markup

Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a css selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.

  1. <button class="btn btn-danger" data-toggle="collapse" data-target="#demo">
  2. simple collapsible
  3. </button>
  4.  
  5. <div id="demo" class="collapse in"></div>
Heads up! To add accordion-like group management to a collapsible control, add the data attribute data-parent="#selector". Refer to the demo to see this in action.

Methods

.collapse(options)

Activates your content as a collapsible element. Accepts an optional options object.

  1. $('#myCollapsible').collapse({
  2. toggle: false
  3. })

.collapse('toggle')

Toggles a collapsible element to shown or hidden.

.collapse('show')

Shows a collapsible element.

.collapse('hide')

Hides a collapsible element.

Events

Bootstrap's collapse class exposes a few events for hooking into collapse functionality.

Event Description
show This event fires immediately when the show instance method is called.
shown This event is fired when a collapse element has been made visible to the user (will wait for css transitions to complete).
hide This event is fired immediately when the hide method has been called.
hidden This event is fired when a collapse element has been hidden from the user (will wait for css transitions to complete).
  1. $('#myCollapsible').on('hidden', function () {
  2. // do something…
  3. })

About

A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

Download file

Example

Start typing in the field below to show the typeahead results.


Using bootstrap-typeahead.js

Call the typeahead via javascript:

  1. $('.typeahead').typeahead()

Options

Name type default description
source array [ ] The data source to query against.
items number 8 The max number of items to display in the dropdown.
matcher function case insensitive The method used to determine if a query matches an item. Accepts a single argument, the item against which to test the query. Access the current query with this.query. Return a boolean true if query is a match.
sorter function exact match,
case sensitive,
case insensitive
Method used to sort autocomplete results. Accepts a single argument items and has the scope of the typeahead instance. Reference the current query with this.query.
highlighter function highlights all default matches Method used to highlight autocomplete results. Accepts a single argument item and has the scope of the typeahead instance. Should return html.

Markup

Add data attributes to register an element with typeahead functionality.

  1. <input type="text" data-provide="typeahead">

Methods

.typeahead(options)

Initializes an input with a typeahead.