Skip to content

Instantly share code, notes, and snippets.

@FabianSchmick
Created May 11, 2020 09:44
Show Gist options
  • Save FabianSchmick/df1b890ad03dfb307b52155e15973d2a to your computer and use it in GitHub Desktop.
Save FabianSchmick/df1b890ad03dfb307b52155e15973d2a to your computer and use it in GitHub Desktop.
Reset radio - switch buttons
/**
* Initializes switch fields
*
* https://stackoverflow.com/a/8318129/5947371
*/
initSwitch() {
let selector = '.custom-control-input[type="radio"]';
$(selector).each((index, radio) => {
let $radio = $(radio),
$label = $('label[for="'+radio.id+'"]');
$label.add(radio).mousedown(function () {
$radio.data('checked', radio.checked);
});
$label.add(radio).click(function () {
if ($radio.data('checked')) {
$radio.prop('checked', false);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment