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
Dinesh123Dinesh123 

Command button to be executed when Enter key is hit

Hi All,

           I have a VF page where we have a command button and if we click the button the functionality is working but we want the same functionality to work when we hit enter. Basically hitting enter key should make the command button work like we clicked it . Can some one guide me how to get this done.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
Exmaple 1:-
 
Teach_me_how
try this. credits to sandersen
 
<apex:form id="searchForm">
<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('contactMergePage:searchForm:searchButton');
ele.click();
    return false;
    } else{
    return true;
    }
   }
</script>
<apex:inputText value="{!searchText}" id="searchText" onkeypress="return noenter(event);"/>
<apex:commandButton id="searchButton" action="{!search}" rerender="searchresults,selectedContacts,winnerpanel" value="Search" status="searchStatus"/>
<apex:actionStatus startText="(searching...)" id="searchStatus"/>
</apex:form>

Exmaple 2:- 
Teach_me_how
try this. credits to sandersen
 
<apex:form id="searchForm">
<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('contactMergePage:searchForm:searchButton');
ele.click();
    return false;
    } else{
    return true;
    }
   }
</script>
<apex:inputText value="{!searchText}" id="searchText" onkeypress="return noenter(event);"/>
<apex:commandButton id="searchButton" action="{!search}" rerender="searchresults,selectedContacts,winnerpanel" value="Search" status="searchStatus"/>
<apex:actionStatus startText="(searching...)" id="searchStatus"/>
</apex:form>

http://salesforce.stackexchange.com/questions/42091/make-enter-key-execute-controller-method
http://bobbuzzard.blogspot.in/2012/10/press-enter-to-submit.html

Example 3:-
<apex:inputText (your args) onkeydown="if(event.keyCode==13){this.blur();actionFunction();}" />
and add below action function
<apex:actionFunction name='doSearchAF' action='{!doSearch}' />
http://developer.force.com/cookbook/recipe/submit-a-form-with-the-enter-key



Please let us know if this will help you

Thanks
Amit Chaudhary