Enter Key

How to Disable Enter Key Without jQuery or Plugin

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 hitting enter button in 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 these code on your theme’s functions.php file.

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

Spread the love

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

Morshed Alam

You use WordPress! Why don't we share our experience! It may be a tutorial, tips, tricks or about security, performance or WordPress news. Write Today

Leave a Comment

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