Skip to content

Instantly share code, notes, and snippets.

@jipiboily
Last active July 4, 2018 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jipiboily/cfd8494fe174eb2a7f6202220196492a to your computer and use it in GitHub Desktop.
Save jipiboily/cfd8494fe174eb2a7f6202220196492a to your computer and use it in GitHub Desktop.
Bulma: radio buttons as buttons
<div class="control field m-t-md radio-buttons-as-buttons">
<label class="label" for="goal_short_or_long_term">Court ou long terme?</label>
<label class='button'>
<input type="radio" value="short_term" name="goal[short_or_long_term]" id="goal_short_or_long_term_short_term" />
Court
</label>
<label class='button'>
<input type="radio" value="medium_term" name="goal[short_or_long_term]" id="goal_short_or_long_term_medium_term" />
Moyen
</label>
<label class='button'>
<input type="radio" value="long_term" checked="checked" name="goal[short_or_long_term]" id="goal_short_or_long_term_long_term" />
Long
</label>
</div>
// if you use turbolink, replace DOMContentLoaded by turbolinks:load
document.addEventListener("DOMContentLoaded", function() {
var inputs = document.querySelectorAll('.radio-buttons-as-buttons input')
Array.from(inputs).forEach(function(element){
element.addEventListener('click', function(event){
// remove is-info to the current active button of this group (if there's one)
event.target.parentElement.parentElement.querySelectorAll('label').forEach(function(el){el.classList.remove('is-info')})
// add is-info to the new active
event.target.parentElement.classList.add('is-info')
});
})
});
.radio-buttons-as-buttons {
label {
@extend button
}
input {
display: none;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment