Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-toggle="button" initial active state #11535

Closed
aaronchi opened this issue Nov 19, 2013 · 7 comments
Closed

data-toggle="button" initial active state #11535

aaronchi opened this issue Nov 19, 2013 · 7 comments
Labels

Comments

@aaronchi
Copy link

Using data-toggle="buttons" doesn't set the initial active state of the radio/checkbox buttons if they are already checked.

In the following example, the first button should get a class of active when the widget is initialized, but it does not.

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary">
    <input type="radio" name="options" id="option1" checked="checked"> 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>
@johnnyshields
Copy link

👍 +1 having the same issue

@johnnyshields
Copy link

Here's a quick coffeescript hack for those who need it:

class window.BsHack
  @fix_btn_toggle: ->
    $('[data-toggle="buttons"]').each (i, grp) ->
      $(grp).children('label.btn').each (i, btn) ->
        btn = $(btn)
        btn.addClass('active') if btn.children('input').first().attr('checked')=='checked'

@johnnyshields
Copy link

At a minimum, please provide us a JS function which will set the correct button state across the page, which we can call at page load time.

@fat
Copy link
Member

fat commented Dec 24, 2013

you should be setting this in your template… same place you are setting checked=checked

but for some reason if you can't access your template… just include this:

$(function () {
  // you may want to add an additional check to make sure that the parent is the element you expect it to be
  $('[checked="checked"]').parent().addClass('active')
})

@fat fat closed this as completed Dec 24, 2013
@aaronchi
Copy link
Author

Why not add this to the library though?

@cvrebert
Copy link
Collaborator

One possible reason is that I don't think there's a non-super-hacky way (at least without overhauling the plugin) to make that also work for elements added to the DOM after the initial page load, which would be a confusing/annoying inconsistency.

@cvrebert
Copy link
Collaborator

cvrebert commented Feb 8, 2014

Duplicate of #8919.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants