Created
April 10, 2020 19:35
-
-
Save brandonhellman/9d7800a0e13fb76352f220b090b586a7 to your computer and use it in GitHub Desktop.
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
// ==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