Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brandonhellman/9d7800a0e13fb76352f220b090b586a7 to your computer and use it in GitHub Desktop.
Save brandonhellman/9d7800a0e13fb76352f220b090b586a7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Scroll To and Highlight First Empty Radio
// @namespace https://github.com/Kadauchi
// @version 1.0.0
// @description Scrolls to and highlights the first empty radio on a keypress
// @author Kadauchi
// @include *
// ==/UserScript==
const key = '`'; // Go to http://keycode.info/ and press a key to change (you want event.key)
function scrollToAndHighlightEmptyRadio() {
const radios = document.querySelectorAll('input[type="radio"]');
const radioGroupNames = [...radios].reduce((names, radio) => names.includes(radio.name) ? names : [...names, radio.name], []);
const radioGroupEmpty = radioGroupNames.find((name) => !document.querySelector(`input[type="radio"][name="${name}"]:checked`));
if (radioGroupEmpty) {
const firstRadioInGroup = document.querySelector(`input[type="radio"][name="${radioGroupEmpty}"]`);
firstRadioInGroup.scrollIntoView();
firstRadioInGroup.parentElement.style.backgroundColor = 'red';
}
}
document.addEventListener('keydown', (event) => {
if (event.key === key) {
scrollToAndHighlightEmptyRadio();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment