Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
OnKeyPress on Input
Message
From
12/03/2012 14:39:59
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, United States
 
 
To
12/03/2012 08:42:30
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01538034
Message ID:
01538098
Views:
31
This is easy if you use jquery.
$(document).delegate(':input','keypress',function(e){
   if (this.id == 'whatever') {
      document.forms[0].submit(); // or something like that.
    }
})
>By default, if you have an input field with a Input Type=Submit button, this will make it a post if you hit Enter in an input field of the form. This works ok for as long as the submission control is as indicated. However, if you have a graphical toolbar for the Save/Cancel, for example, in order to simulate the same, we have to use the OnKeyPress event of an input field and assign a javascript function which will determine if the Enter key was press so to submit the form.
>
>Instead of adding this OnKeyPress event to every input field, would there be a way to have it generic? I can assign document.onkeypress=FormKeyPress, which will be the javascript function. But, this will also trigger a call to this function whenever an Enter key is pressed, even if this is in a textarea field. In that case, it should not post the form. So, based on that approach, the one which adds an event at the form level, is there a way to detect that the click was done in an input field or a textarea field so to avoid doing anything if the click was done in a textarea field?
>
>Note that onkeypress is recommanded to be replaced by onkeydown so it will work with all browsers as some of them intercept the ESC key, such as Chrome, and they will not hit the event.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform