hebrerillo
Active Coder
Hello everyone!
I want to know if there is a way to check if an input is focusable or not via JavaScript.
I am implementing a form, and when the user hits the enter key, the focus should go to the next "focusable" input via JavaScript. The problem is that some inputs are hidden via "display:none", and they cannot be focused.
I attach an example code:
Thank you in advance!!
I want to know if there is a way to check if an input is focusable or not via JavaScript.
I am implementing a form, and when the user hits the enter key, the focus should go to the next "focusable" input via JavaScript. The problem is that some inputs are hidden via "display:none", and they cannot be focused.
I attach an example code:
JavaScript:
function onKeyDown(event) {
if (event.key !== "Enter") {
return;
}
event.preventDefault();
const target = event.target;
const targetIndex = form.elements.indexOf(target); //Form is the actual form element
if (!target.checkValidity() || targetIndex >= (this.elements.length - 1)) {
return;
}
const nextFocusable = this.elements[targetIndex + 1];
nextFocusable.focus(); //If the input is hidden, this funcion has no effect.
}
Thank you in advance!!