Pressing enter in a text box submits the form

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Pressing enter in a text box submits the form

#1

Postby Segi » Tue Dec 17, 2013 5:18 pm

I've found a very serious bug in eBase which I have been able to reproduce in a new form.

When you press enter in a text field, the focus jumps to the button and triggers its click event.

You can reproduce this by creating a new project, add a field and a button and run the application. Click on the text field press enter. The focus jumps to the button which is clear because it is has an orange border indicating that it has the focus. This will also trigger the click event script if there is one assigned.

Please advise me if there is a way to disable this behavior but it causes unwanted side effects
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

#2

Postby Segi » Tue Dec 17, 2013 5:46 pm

To add to my last post,

I've been looking into it further. When you hit enter in a text field, the page reloads. Is there a way to prevent this behavior ?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#3

Postby Jon » Wed Dec 18, 2013 8:43 am

What you're seeing here is the default behaviour of most, but not all, browsers. They interpret the enter key as meaning a submit of the form and do this by "clicking" the next button. Seeing as how this is common web behaviour, many people expect this so you should be a little bit careful about removing it.

You can suppress this behaviour by adding the following Javascript to a page. If you want to do this for all forms, it's probably easiest to create a client script and add this to the presentation template. This uses jQuery so the jQuery core javascript also needs to be loaded.

Code: Select all

$(document).keypress(function (e) {
  if(e.which == 13 && e.target.nodeName != "TEXTAREA") return false;
});
Here's an alternative version found online that doesn't use jQuery:

Code: Select all

function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
0 x


Who is online

Users browsing this forum: No registered users and 6 guests