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

Bootstrap Button Plugin

Bootstrap Button Plugin

The Button plugin is introduced in the Bootstrap Buttons chapter. With the Button plugin, you can add interactions such as controlling button states, or creating button groups for other components (such as toolbars).

Loading state

To add a loading state to a button, simply add data-loading-text="Loading..." as an attribute of the button element, as shown in the following example:

Example

<button id="fat-btn" class=">

btn btn-primary

"

data-loading-text

=

"

Loading...

"

type

=

"

button

"

Loading state

button

<

script

$(function() { $(".btn").click(function(){ $(this).button('loading').delay(1000).queue(function() { // $(this).button('reset'); // $(this).dequeue(); }); }); });

script

Try it »

The result is as follows:

Single toggle

To activate toggling on a single button (that is, changing the button's normal state to a pressed state and vice versa), simply add data-toggle="button" as an attribute of the button element, as shown in the following example:

Example

<button type="button" class=">

btn btn-primary

"

data-toggle

=

"

button

"

Single toggle

button

Try it »

The result is as follows:

Checkbox

You can create a checkbox group and enable toggling for it by adding btn-group data attributes to the data-toggle="buttons" .

Example

<div class="btn-group" data-toggle="buttons">
<label class=">

btn btn-primary

"

<input type="checkbox">

Option 1

label

<label class=">

btn btn-primary

"

<input type="checkbox">

Option 2

label

<label class=">

btn btn-primary

"

<input type="checkbox">

Option 3

label

div

Try it »

The result is as follows:

Radio buttons

Similarly, you can create a radio button group and enable toggling for it by adding btn-group data attributes to the data-toggle="buttons" .

Example

<div class="btn-group" data-toggle="buttons">
<label class=">

btn btn-primary

"

<input type="radio" name="options" id="option1">

Option 1

label

<label class=">

btn btn-primary

"

<input type="radio" name="options" id="option2">

Option 2

label

<label class=">

btn btn-primary

"

<input type="radio" name="options" id="option3">

Option 3

label

div

Try it »

The result is as follows:

Usage

You can via JavaScript enable the Button plugin, as shown below:



$('.btn').button()

Option

No options.

Method

The following are some useful methods of the Button plugin:

Method Description Example
button('toggle') Toggles the pressed state. Gives the button an activated appearance. You can use the data-toggle attribute to enable automatic toggling of a button.
$().button('toggle')
.button('loading') While loading, the button is disabled and the text becomes that of the button element's data-loading-text the value of the property.
$().button('loading')
.button('reset') Resets the button state and restores the text content to its original content. This method is very useful when you want to return the button to its original state.
$().button('reset')
.button(string) The string in this method refers to any string declared by the user. Using this method, reset the button state and add new content.
$().button(string)

Example

The following example demonstrates the use of the above methods:

Example

<

h2

Click each button to see the method's effect

h2

<

h4

Demonstrate the .button('toggle') method

h4

<div id="myButtons1" class="bs-example">
<button type="button" class=">

btn btn-primary

"

Original

button

div

<

h4

Demonstrate the .button('loading') method

h4

<div id="myButtons2" class="bs-example">
<button type="button" class=">

btn btn-primary

"

data-loading-text

=

"

Loading...

"

Original

button

div

<

h4

Demonstrate the .button('reset') method

h4

<div id="myButtons3" class="bs-example">
<button type="button" class=">

btn btn-primary

"

data-loading-text

=

"

Loading...

"

Original

button

div

<

h4

Demonstrate the .button(string) method

h4

<button type="button" class=">

btn btn-primary

"

id

=

"

myButton4

"

data-complete-text

=

"

Loading finished

"

Please click me

button

<script$(function(){$(">

#myButtons1 .btn

"

)

.

click

(

function

(

)

{

$

(

this

)

.

button

(

'

toggle

'

)

;

}

)

;

}

)

; $

(

function

(

)

{

$

(

"

#myButtons2 .btn

"

)

.

click

(

function

(

)

{

$

(

this

)

.

button

(

'

loading

'

)

.

delay

(

1000

)

.

queue

(

function

(

)

{

}

)

;

}

)

;

}

)

; $

(

function

(

)

{

$

(

"

#myButtons3 .btn

"

)

.

click

(

function

(

)

{

$

(

this

)

.

button

(

'

loading

'

)

.

delay

(

1000

)

.

queue

(

function

(

)

{

$

(

this

)

.

button

(

'

reset

'

)

;

}

)

;

}

)

;

}

)

; $

(

function

(

)

{

$

(

"

#myButton4

"

)

.

click

(

function

(

)

{

$

(

this

)

.

button

(

'

loading

'

)

.

delay

(

1000

)

.

queue

(

function

(

)

{

$

(

this

)

.

button

(

'

complete

'

)

;

}

)

;

}

)

;

}

)

;

script

Try it »

The result is as follows: