• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Dynamic Forms > 8.8. Submitting a Form by an Enter Key Press in Any Text Box

Submitting a Form by an Enter Key Press in Any Text Box

NN 4, IE 4

Problem

You want a press of the Return/Enter key in one or more text fields to submit the form.

Solution

To simulate a standalone database entry form, you might implement the auto-focusing technique in Recipe 8.6 for all but the last test box, which instead uses the onkeypress event handler to invoke the following function:

function submitViaEnter(evt) {
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var form = target.form;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode =  = 13 || charCode =  = 3) {
        if (validateForm(form)) {
            form.submit( );
            return false;
        }
    }
    return true;
}

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint