Home » Tutorial » How to Disable Enter Key Without jQuery or Plugin

How to Disable Enter Key Without jQuery or Plugin

Enter Key

If you search on the internet about How to Stop the Enter Key from Submitting the Form, you will find some jQuery solutions.

Those solutions are good. But the problem is that maximum solutions will prevent users from hitting the enter button in the textarea. So users won’t be able to create a new line in the comment section.

And the main problem is that those solutions are dependent on jQuery.

Some of your pages may have jQuery loaded, and some may not. Running jQuery on those pages just to disable the enter key is not a good idea. Or you may not use jQuery at all.

So I have discovered pure JavaScript to do that. Here it is:

add_action('wp_footer', 'dgwt_disable_form_submit_on_enter', 99);
function dgwt_disable_form_submit_on_enter(){ ?>
 document.onkeypress = function(evt) {
  if(evt.target.toString() !== '[object HTMLTextAreaElement]' && evt.charCode === 13){
<?php }

Paste this code into your theme’s functions.php file.

Let me know in the comment section if it works or not.

Read more from Tutorial

Written by:

Morshed Alam
A teacher by profession, a traveler by passion and a netizen by choice.

Have you written on ThoughtMight?Write Today

Leave a Comment

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