YouTip LogoYouTip

Bootstrap Button Plugin

The Button was introduced in the (#) chapter. With the Button plugin, you can add some interactivity, such as controlling button states, or creating button groups for other components (like toolbars). ## Loading State To add a loading state to a button, simply add the **data-loading-text="Loading..."** attribute to the button element, as shown in the following example: ## Example $(function() { $(".btn").click(function(){ $(this).button('loading').delay(1000).queue(function() { // $(this).button('reset'); // $(this).dequeue(); }); }); }); [Try it Β»](#) The result is as follows: ![Image 1: Button Plugin Loading State](#) ## Single Toggle To activate the toggle for a single button (i.e., change the button's normal state to a pressed state, and vice versa), simply add the **data-toggle="button"** attribute to the button element, as shown in the following example: ## Example [Try it Β»](#) The result is as follows: ![Image 2: Button Plugin Single Toggle](#) ## Checkbox You can create a checkbox group and add toggle functionality to the checkbox group by adding the data attribute **data-toggle="buttons"** to the **btn-group**. ## Example
[Try it Β»](#) The result is as follows: ![Image 3: Button Plugin Checkbox](#) ## Radio Similarly, you can create a radio button group and add toggle functionality to the radio button group by adding the data attribute **data-toggle="buttons"** to the **btn-group**. ## Example
[Try it Β»](#) The result is as follows: ![Image 4: Button Plugin Radio](#) ## Usage You can enable the Button plugin **via JavaScript**, as follows: $('.btn').button() ## Options _No options._ ## Methods Here are some useful methods for the Button plugin: | Method | Description | Example | | --- | --- | --- | | button('toggle') | Toggles the pressed state. Gives the button the appearance of being activated. You can enable automatic toggling of the button using the **data-toggle** attribute. | $().button('toggle') | | .button('loading') | When loading, the button is disabled, and the text becomes the value of the button element's **data-loading-text** attribute. | $().button('loading') | | .button('reset') | Resets the button state, and the text content reverts to the 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, resets the button state and adds new content. | $().button(string) | ### Example The following example demonstrates the usage of the methods above: ## Example

Click each button to see the method effect

Demonstrate .button('toggle') method

Demonstrate .button('loading') method

Demonstrate .button('reset') method

Demonstrate .button(string) method

$(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'); }); }); }); [Try it Β»](#) The result is as follows: ![Image 5: Button Plugin Methods](#)
← Bootstrap Collapse PluginBootstrap Alert Plugin β†’