Submit form on key press (keyup)

I want to be able to submit a form on pressing Ctrl+Shift+s. I’ve added the following to my JS but nothing - no errors, nothing in the console, no form submission, and other keystrokes still work.

document.addEventListener("keyup", function(e) {
  if (e.ctrlKey && e.shiftKey && e.key === "s")
    document.getElementById("form").submit();
  // check for other keystrokes
});

Am I missing something? What am I missing?

The keyup event doesn’t affect the document, especially with the keys you’re listening for.

Are you wanting window.addEventListener instead?

1 Like

I’ll give that a go, thanks Paul.

document.addEventListener(“keyup”…) works for ctrl+b and alt+3.

Would you suggest a different key combination for submit?

Without testing, which I can’t do for another 12 hours, it could also be because the last form field can have trouble when triggering events.

Can you please supply a test page that exhibits the trouble, so that we have as much information as practical about your situation?

1 Like

Will do. It will take me a while, as it’s a password-protected admin page…

1 Like

Okay, so I seem to have resolved it. Changing

if (e.ctrlKey && e.shiftKey && e.key === "s")

to

if (e.ctrlKey && e.key === "S")

seemed to do the trick!

1 Like

Now that I’ve been able to check this out, your original code works when using a capital S instead of a lowercase s.

document.addEventListener("keyup", function(e) {
  if (e.ctrlKey && e.shiftKey && e.key === "S")
    document.getElementById("form").submit();
  // check for other keystrokes
});

The shift key is being held down so it’s a capital letter instead :slight_smile:

2 Likes

Der, yes! Thanks.

And thus the && e.shiftKey is redundant.

1 Like

I wouldn’t say that the shift key is redundant. It helps to make your intentions much clearer in the code, that you expect the person to use ctrl+shift+key.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.