Last active
May 13, 2024 07:51
-
-
Save Fanoflix/e62011f378cdd84cd790b322fc114688 to your computer and use it in GitHub Desktop.
Ctrl + Enter to submit Form (Typescript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.body.addEventListener("keydown", (e: KeyboardEvent) => { | |
if (!(e.key === "Enter" && (e.metaKey || e.ctrlKey))) return | |
const form = (e.target as HTMLFormElement).form | |
if (form) form.submit() // or form.requestSubmit() depending on your usecase | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, the
![image](https://private-user-images.githubusercontent.com/68850090/327435973-a58975e8-9b5c-4438-ba0e-c047ff1bb47f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxNTEzMzIsIm5iZiI6MTcyMjE1MTAzMiwicGF0aCI6Ii82ODg1MDA5MC8zMjc0MzU5NzMtYTU4OTc1ZTgtOWI1Yy00NDM4LWJhMGUtYzA0N2ZmMWJiNDdmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI4VDA3MTcxMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk5ZTdkNzZjMGY5MDI1YTdmZmFkNjY0ZWNlYTFjOGNjNTcxNjhjNjlkZmE4YTVkZjE1ZGFiNTRjNTllMjc1ZGYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.0FGBUy-RA-MProEpiGcA1R9T5o8PnLksMkinYvYx7rE)
e.target as HTMLFormElement
isn't the most precise coercion, because the.form
receives theany
type:A bit more robust handling would look like this: