Skip to content
All gists
Back to GitHub
Sign in
Sign up
Sign in
Sign up
{{ message }}
Instantly share code, notes, and snippets.
claudia-romano
/
custom-checkbox-radio-buttons.js
Last active
November 8, 2017 14:13
Star
0
Fork
0
Star
Code
Revisions
2
Embed
What would you like to do?
Embed
Embed this gist in your website.
Share
Copy sharable link for this gist.
Clone via HTTPS
Clone with Git or checkout with SVN using the repository’s web address.
Learn more about clone URLs
Download ZIP
Raw
custom-checkbox-radio-buttons.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
(function() {
function ncCreateCustomEvent(eventName) {
//this function is used to create custom events in Javascript
//you can use your own if you already have one
var customEvent;
if (typeof window.Event == 'function') {
customEvent = new Event(eventName, {'bubbles': false, 'cancelable': true});
} else {
//fallback for IE - initEvent is deprecated but this will run only for IE9/IE10
customEvent = document.createEvent('Event');
customEvent.initEvent(eventName, false, true);
}
return customEvent;
}
function ncCheckOption(event) {
//an input has been selected/deselected -> we need to trigger the icon animation
event.target.nextElementSibling.getElementsByTagName('svg')[0].dispatchEvent(ncCreateCustomEvent('animate'));
//if the input is a radio input, we need to deselect all the siblings
if(event.target.type == 'radio') ncResetRadio(this, event);
}
function ncResetRadio(wrapper, input) {
var radioInput = wrapper.querySelectorAll('input[type=radio]:not(:checked)'),
resetEvent = ncCreateCustomEvent('reset');
for( var i = 0; radioInput.length > i; i++ ) {
radioInput[i].nextElementSibling.getElementsByTagName('svg')[0].dispatchEvent(resetEvent);
}
}
var formButtons = document.getElementsByClassName('js-nc-form-buttons');
if( formButtons ) {
for( var i = 0; formButtons.length > i; i++ ) {
formButtons[i].addEventListener('change', ncCheckOption);
}
}
}());
Sign up for free
to join this conversation on GitHub
. Already have an account?
Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.