+ Start a Discussion
MarceldeBMarceldeB 

Commandbutton on enter

Hi,

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"/>
      </apex:pageblockbuttons>

 ps: in dutch opslaan = save, annuleren = cancel

 

Pradeep_NavatarPradeep_Navatar

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){              

                                                if(window.event){                   

                                                                key = window.event.keyCode;     //IE               

                                                }

                                                else{                   

                                                                key = e.which;     //firefox              

                                                }              

                                                if(key == 13) {                   

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

                                                                ele.click();                    

                                                                return false;              

                                                } else{                   

                                                                return true;              

                                                }         

                                }    

                         </script>    

 

Hope this helps.

MarceldeBMarceldeB

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?