Disable Enter to save?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
SilentNight
User
Posts: 171

Disable Enter to save?

Post by SilentNight »

How can I disable pressing enter on the keyboard to save a record on Add/Copy and Edit pages?


mobhar
User
Posts: 11660

Post by mobhar »

You may use jQuery .keydown() to detect Enter key, and then call preventDefault event.


SilentNight
User
Posts: 171

Post by SilentNight »

Thanks!

This did the trick:

Client Scripts -> Table-Specific -> Add/Copy Page/Edit Page -> Startup Script
// Prevent saving when enter is pressed
$(document).ready(function() {
  $(window).keydown(function(e){
    if(event.keyCode == 13) {
      e.preventDefault();
      return false;
    }
  });
});

SilentNight
User
Posts: 171

Post by SilentNight »

Bringing back this thread because it appears the code I posted above prevents pressing enter to add line breaks in textarea fields which is a problem. Any ideas around this?


arbei
User
Posts: 9292

Post by arbei »

You may check the "e" event in your code to check the source element. View the event by console.log(e) in your browser first.


SilentNight
User
Posts: 171

Post by SilentNight »

Thank you for pointing me in the right direction! e.srcElement always returned undefined but I found e.target worked so I changed the code above to:

// Prevent saving when enter is pressed
$(document).ready(function() {
  $(window).keydown(function(e){
    if (e.target.type !== "textarea"){
        if(event.keyCode == 13) {
        e.preventDefault();
        return false;
        }
    }
  });
});

This ignores textarea fields. All is working now.


Post Reply