function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Commandbutton on enter


I have a VF page with two commandbuttons (save and cancel). If a user presses the enter key halfway editing the page, the page performs the cancel command and all data is lost, while I would want the save command to be called. i searched the docu and forum but see no simple way to control the priority of the commandbuttons or the setting to link a commandbutton to the enter key. any tips?



      <apex:pageblockbuttons location="top">
        <apex:commandButton value="Opslaan" action="{!saveall}" tabindex="0" rerender="mdw,arb"/>
        <apex:commandButton value="Annuleren" action="{!cancelall}" immediate="true"/>

 ps: in dutch opslaan = save, annuleren = cancel



Have you handled the key event(Enter Key)? Add the following attribute to your key event and use javascript that takes appropriate action. Find below the sample code :


            <script type="text/javascript">         

                                function noenter(e){              


                                                                key = window.event.keyCode;     //IE               



                                                                key = e.which;     //firefox              


                                                if(key == 13) {                   

                                                                var ele=document.getElementById('page:crit:gobutton');                                                                                                                                             


                                                                return false;              

                                                } else{                   

                                                                return true;              





Hope this helps.


Hi, thanks for your support.

I put the script in and added a generic call in the first line of the script:


window.onkeypress = noenter;

and see the function is called (by adding a message to the script), but no saving yet. I changed the component call to:



var ele=document.getElementById('{!$component.savebutton}');


where savebutton is the id of my button, but still he doesn't seem to find it. What am I doing wrong with the button component-reference?