Submit Form by CTRL Enter or Other Custom Keys using jQuery

arp14 jQuery, Tutorial Tags: , , , , , , 1

You have seen on many websites, after write some text within a textarea and pressing enter – the form got submitted. The key combination can be Shift Enter, CTRL Enter or anything. In the script below I’m showing you how to do that.

Submit Form By CTRL Enter

<script>
window.onload = function () {
	document.getElementById("editor_form").onkeydown = function (e) {
		if (e.keyCode == 13 && e.ctrlKey) { // keyCode 13 is Enter
			document.getElementById("submit").click(); // submit the form by hitting ctrl + enter
			// alert(e.keyCode); // to know other keyCodes of each keys
			return false; // preventing default action
		}
	}
}
</script>

In the script above, we’re calling a standalone function on the window.onload event. Then we’re getting the textarea – from where the key combination will be taken. Then on onkeydown event, we’re calling another standalone function with a event parameter. After that, we’re getting the keys got pressed on keyboard using e.keyCode function. To check if CTRL Enter is pressed of not we’re using if-else statement with condition e.keyCode == 13 && e.ctrlKey. Then if CTRL Enter got pressed, we’re clicking on submit button and returning false to prevent default action by CTRL Enter. To check key codes of each keys, just uncomment the sixth line and run the code – press any key you want to know.

In jQuery, the script will be like this:

<script>
$(document).ready(function() {
	$("#textarea_id").keydown(function (e) {
		if (e.keyCode == 83 && e.ctrlKey) {
			$("#submit").click();
			return false;
		}
	});
});
</script>

Having problem? Need help? Please comment below and give your feedback, I will be back to you as soon as possible – Thank You.

One thought on “Submit Form by CTRL Enter or Other Custom Keys using jQuery

Leave a Reply

Note: Your email address will not be published. Required fields are marked *