- Joined
- Jan 12, 2016
- Messages
- 7,098
- Reaction score
- 21,951
- Points
- 1,263
Here, I will try to make this as easy to understand as possible.what do you mean?
I know very little in this type of language
Code:
// This is your "counter" variable
let idx = 0;
// This adds a listener when you press a key.
document.addEventListener('keydown', (e) => {
switch (e.which) {
// If the 1 (case 49) key or Numpad1 (case 97) key are pressed.
case 49: case 97:
// Everything is zero indexed, so the first radio is 0, the second is 1 and so on.
// $(':even') will get all of the radios that are 0,2,4,6.... So being zero indexed means..
// it will choose the 1st, 3rd, 5th, 7th....
// .eq(idx ++) will choose which of the :even radios, so $(':radio:even').eq(2) will choose the 5th radio.
// .click() will click on the element.
// So we are getting all of the even radios, then choosing which we want out of those and clicking it, then adding +1 to the index.
$(':radio:even').eq(idx ++).click();
// This will focus the next radio, scrolling the page if necessary
$(':radio:even').eq(idx).focus();
break;
// If the 2 (case 50) key or Numpad2 (case 98) key are pressed.
case 49: case 97:
// Same as above, but odds instead of evens.
$(':radio:odd').eq(idx ++).click();
// This will focus the next radio, scrolling the page if necessary
$(':radio:even').eq(idx).focus();
break;
default: return;
}
});