0

I have a series of text inputs wherein when the user presses space, that input is intercepted and the site treats it as it would normally treat the tab button being pressed. Meaning, instead of typing an actual space character, the space character is intercepted and prevented from being entered into the input, and instead focus is shifted to the next input according to the tab index as would normally happen when someone presses tab.

Intercepting the space character is not a problem, but I am not sure how to emulate a tab press afterwards. I want to avoid having to manually keep state of what input is in focus and figuring out what is appropriate to set focus to next. The browser already does that for me when I press tab, and I feel like there should be a way to manually invoke the functionality that we associate with the tab button, but I can't find any mention of it.

  • Could I transform the "space" keypress into a "tab" keypress?
  • Does pressing tab dispatch some kind of built-in event that I could also dispatch manually?
  • Is there some window function that does what I need?
  • Or is keeping track of focus and manually calling focus() on the next element truly the only way?

I'm open to anything. Libraries or otherwise. I'm using svelte if that matters.

Please no jQuery.

JSON Brody
  • 736
  • 1
  • 4
  • 23
  • You might have some luck in this thread: https://stackoverflow.com/questions/7208161/focus-next-element-in-tab-index. Try the second answer. You could add an event listener for the space button, and when it registers, you could call the function that has been provided in the answer – Omar Siddiqui Nov 03 '22 at 04:47

0 Answers0