buttons.html 6.31 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
2
3
<div class="bs-docs-section">
  <h1 id="buttons" class="page-header">Buttons <small>button.js</small></h1>

4
  <p class="lead">Do more with buttons. Control button states or create groups of buttons for more components like toolbars.</p>
Mark Otto's avatar
Mark Otto committed
5

Chris Rebert's avatar
Chris Rebert committed
6
  <div class="bs-callout bs-callout-danger" id="callout-buttons-ff-autocomplete">
7
    <h4>Cross-browser compatibility</h4>
8
    <p><a href="https://github.com/twbs/bootstrap/issues/793">Firefox persists form control states (disabledness and checkedness) across page loads</a>. A workaround for this is to use <code>autocomplete="off"</code>. See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=654072">Mozilla bug #654072</a>.</p>
9
10
11
  </div>

  <h2 id="buttons-stateful">Stateful</h2>
Mark Otto's avatar
Mark Otto committed
12
  <p>Add <code>data-loading-text="Loading..."</code> to use a loading state on a button.</p>
13
  <p><strong class="text-danger">This feature is deprecated since v3.3.5 and will be removed in v4.</strong></p>
Chris Rebert's avatar
Chris Rebert committed
14
  <div class="bs-callout bs-callout-info" id="callout-buttons-state-names">
15
16
17
18
19
    <h4>Use whichever state you like!</h4>
    <p>For the sake of this demonstration, we are using <code>data-loading-text</code> and <code>$().button('loading')</code>, but that's not the only state you can use. <a href="#buttons-methods">See more on this below in the <code>$().button(string)</code> documentation</a>.</p>
  </div>
  <div class="bs-example">
    <button type="button" id="loading-example-btn" data-loading-text="Loading..." class="btn btn-primary" autocomplete="off">
Mark Otto's avatar
Mark Otto committed
20
21
22
23
      Loading state
    </button>
  </div><!-- /example -->
{% highlight html %}
24
<button type="button" id="myButton" data-loading-text="Loading..." class="btn btn-primary" autocomplete="off">
Mark Otto's avatar
Mark Otto committed
25
26
  Loading state
</button>
27

Mark Otto's avatar
Mark Otto committed
28
<script>
29
30
31
32
33
  $('#myButton').on('click', function () {
    var $btn = $(this).button('loading')
    // business logic...
    $btn.button('reset')
  })
Mark Otto's avatar
Mark Otto committed
34
35
36
</script>
{% endhighlight %}

37
  <h2 id="buttons-single-toggle">Single toggle</h2>
Mark Otto's avatar
Mark Otto committed
38
  <p>Add <code>data-toggle="button"</code> to activate toggling on a single button.</p>
Chris Rebert's avatar
Chris Rebert committed
39
  <div class="bs-callout bs-callout-warning" id="callout-buttons-single-pretoggled">
40
41
42
    <h4>Pre-toggled buttons need <code>.active</code> and <code>aria-pressed="true"</code></h4>
    <p>For pre-toggled buttons, you must add the <code>.active</code> class and the <code>aria-pressed="true"</code> attribute to the <code>button</code> yourself.</p>
  </div>
43
  <div class="bs-example">
44
    <button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
45
46
      Single toggle
    </button>
Mark Otto's avatar
Mark Otto committed
47
48
  </div><!-- /example -->
{% highlight html %}
49
<button type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
50
51
  Single toggle
</button>
Mark Otto's avatar
Mark Otto committed
52
53
{% endhighlight %}

54
55
  <h2 id="buttons-checkbox-radio">Checkbox / Radio</h2>
  <p>Add <code>data-toggle="buttons"</code> to a <code>.btn-group</code> containing checkbox or radio inputs to enable toggling in their respective styles.</p>
Chris Rebert's avatar
Chris Rebert committed
56
  <div class="bs-callout bs-callout-warning" id="callout-buttons-multi-preselected">
57
58
    <h4>Preselected options need <code>.active</code></h4>
    <p>For preselected options, you must add the <code>.active</code> class to the input's <code>label</code> yourself.</p>
59
  </div>
Chris Rebert's avatar
Chris Rebert committed
60
  <div class="bs-callout bs-callout-warning" id="callout-buttons-only-click">
61
62
63
    <h4>Visual checked state only updated on click</h4>
    <p>If the checked state of a checkbox button is updated without firing a <code>click</code> event on the button (e.g. via <code>&lt;input type="reset"&gt;</code> or via setting the <code>checked</code> property of the input), you will need to toggle the <code>.active</code> class on the input's <code>label</code> yourself.</p>
  </div>
64
  <div class="bs-example" data-example-id="buttons-checkbox">
Mark Otto's avatar
Mark Otto committed
65
    <div class="btn-group" data-toggle="buttons">
66
      <label class="btn btn-primary active">
67
        <input type="checkbox" checked autocomplete="off"> Checkbox 1 (pre-checked)
Mark Otto's avatar
Mark Otto committed
68
69
      </label>
      <label class="btn btn-primary">
70
        <input type="checkbox" autocomplete="off"> Checkbox 2
Mark Otto's avatar
Mark Otto committed
71
72
      </label>
      <label class="btn btn-primary">
73
        <input type="checkbox" autocomplete="off"> Checkbox 3
Mark Otto's avatar
Mark Otto committed
74
75
76
77
78
      </label>
    </div>
  </div><!-- /example -->
{% highlight html %}
<div class="btn-group" data-toggle="buttons">
79
  <label class="btn btn-primary active">
80
    <input type="checkbox" autocomplete="off" checked> Checkbox 1 (pre-checked)
Mark Otto's avatar
Mark Otto committed
81
82
  </label>
  <label class="btn btn-primary">
83
    <input type="checkbox" autocomplete="off"> Checkbox 2
Mark Otto's avatar
Mark Otto committed
84
85
  </label>
  <label class="btn btn-primary">
86
    <input type="checkbox" autocomplete="off"> Checkbox 3
Mark Otto's avatar
Mark Otto committed
87
88
89
90
  </label>
</div>
{% endhighlight %}

91
  <div class="bs-example" data-example-id="buttons-radio">
Mark Otto's avatar
Mark Otto committed
92
    <div class="btn-group" data-toggle="buttons">
93
      <label class="btn btn-primary active">
94
        <input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
Mark Otto's avatar
Mark Otto committed
95
96
      </label>
      <label class="btn btn-primary">
97
        <input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
Mark Otto's avatar
Mark Otto committed
98
99
      </label>
      <label class="btn btn-primary">
100
        <input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
Mark Otto's avatar
Mark Otto committed
101
102
103
104
105
      </label>
    </div>
  </div><!-- /example -->
{% highlight html %}
<div class="btn-group" data-toggle="buttons">
106
  <label class="btn btn-primary active">
107
    <input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
Mark Otto's avatar
Mark Otto committed
108
109
  </label>
  <label class="btn btn-primary">
110
    <input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
Mark Otto's avatar
Mark Otto committed
111
112
  </label>
  <label class="btn btn-primary">
113
    <input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
Mark Otto's avatar
Mark Otto committed
114
115
116
117
  </label>
</div>
{% endhighlight %}

118
  <h2 id="buttons-methods">Methods</h2>
119
  <h4><code>$().button('toggle')</code></h4>
Mark Otto's avatar
Mark Otto committed
120
121
  <p>Toggles push state. Gives the button the appearance that it has been activated.</p>

122
  <h4><code>$().button('reset')</code></h4>
Mark Otto's avatar
Mark Otto committed
123
124
  <p>Resets button state - swaps text to original text.</p>

125
  <h4><code>$().button(string)</code></h4>
126
  <p>Swaps text to any data defined text state.</p>
127

Mark Otto's avatar
Mark Otto committed
128
{% highlight html %}
129
130
131
132
<button type="button" id="myStateButton" data-complete-text="finished!" class="btn btn-primary" autocomplete="off">
  ...
</button>

Mark Otto's avatar
Mark Otto committed
133
<script>
134
135
136
  $('#myStateButton').on('click', function () {
    $(this).button('complete') // button text will be "finished!"
  })
Mark Otto's avatar
Mark Otto committed
137
138
139
</script>
{% endhighlight %}
</div>