I'm using Bootstrap's JavaScript buttons to style some radio buttons, and I don't seem to be able to set an initial button as checked.
Here is my code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input checked="checked" type="radio" id="match-three" name="options">Three numbers
</label>
<label class="btn btn-info">
<input type="radio" name="options" id="match-four">Four numbers
</label>
</div>
The first button has checked="checked"
, but it isn't being styled as checked.
The buttons are being styled OK after click events, so I'm not sure what is going wrong on initial load.
Bootply version here: http://bootply.com/89566
If you want the Bootstrap button()
component to recognize the checked
inputs initially you need to add some jQuery like..
$('[checked="checked"]').parent().addClass("active");
This will set the active state to the appropriate buttons/labels in the group.
If you're using the HTML 5 doctype, just use checked
without any value:
<label class="btn btn-info active">
<input checked type="radio" id="match-three" name="options">Three numbers
</label>
checked="checked"
should work as well, so you'll have to clarify what's not working for you.
checked
attribute on the input
will ensure correct state on the form fieldactive
class on the label
will set the correct visual stateReference documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With