Skip to content

Instantly share code, notes, and snippets.

@matijs
Created January 26, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matijs/5af365742761f449e027 to your computer and use it in GitHub Desktop.
Save matijs/5af365742761f449e027 to your computer and use it in GitHub Desktop.
Trigger a click on a 'save' button by pressing cmd/ctrl-s in a textarea (adapt as needed)
(function() {
'use strict';
document.addEventListener('keydown', function(event) {
var S = 83,
activeElement = document.activeElement,
saveButtonId,
saveButton;
if ((event.key === S || event.keyCode === S) && (event.metaKey || event.ctrlKey) && activeElement.nodeName === 'TEXTAREA') {
saveButtonId = activeElement.getAttribute('data-save-with');
saveButton = document.getElementById(saveButtonId);
if (saveButton && typeof saveButton.click === 'function') {
event.preventDefault();
saveButton.click();
}
}
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment