Skip to content
Advertisement

Button is being triggered by spacebar after clicked once

I have an input field:

<input id="audio_file" type="file" accept="audio/*" />

my problem is that once I’ve clicked it, if I press space after that it simulates the input being clicked again. How do I stop this?

Advertisement

Answer

Once clicked, the element gets a keyboard focus. And the next time you press the space bar or enter “click” occurs again. If you need to avoid it, simply remove the keyboard focus of the element using the blur() function.

window.onload = () => {
  let input = document.querySelector("input");
  input.addEventListener("click", () => input.blur());
}
<html>
  <head></head>
  <body>
    <input type="file" />
  </body>
</html>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement