1
<apex:form styleClass="container">  
<apex:actionRegion >  
  <div class="col-md-3 col-sm-5 col-xs-5 input-group">
    <apex:inputText value="{!searchString}" styleClass="form-control" html-placeholder="Search" />
    <span class="input-group-btn">
      <apex:commandButton  value="Go" action="{!Search}" rerender="searchResults" styleClass="btn btn-default"></apex:commandButton>
    </span>
  </div>
  <apex:pageBlock id="searchResults"> 
    <apex:pageBlockTable value="{!results}" var="a" id="tblResults" styleClass="table-gray col-md-6 col-sm-12 col-xs-12">
      <apex:column >
        <apex:facet name="header">
          <apex:outputPanel >Name</apex:outputPanel>
        </apex:facet>
        <apex:outputLink value="#" onClick="Javascript:parent.setValue('{!a.Name}','{!inputId}')">{!a.Name}</apex:outputLink>     
      </apex:column>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:actionRegion>
</apex:form>

The problem is that when I type something into the inputText and press 'enter' it does not rerender or preform the action. How can I make it so that the user can press either 'Go' or press 'enter'?

1 Answer 1

5

Catch the enter-key at your inputText with the onkeypress-handler and trigger a fake-click on the button:

<script>
    function handlerKeyPress(ev) {
        if (window.event && window.event.keyCode == 13 || ev.which == 13) {
            $('.myUniqueSubmitButtonClass').click();   
            return false;
        } else {
            return true;
        }
    }
</script>
... 
<apex:inputText value="{!searchString}" onkeypress="return handlerKeyPress(event);" /> 
...
<apex:commandButton styleClass="myUniqueSubmitButtonClass btn btn-default" value="Go" action="{!Search}" rerender="searchResults" />

Note:

  • For the sake of simplicity I use here jquery. For sure you may rewrite it also without.
  • I added "myUniqueSubmitButtonClass" to be able to select to button easily
2
  • While this works when I run it in the console it does not work on page. The problem is that since it is inside of a form when I press enter it submits the form and reloads the page. The way around it is press 'Go' since go does not submit the form. Is there a way to modify your code so that it will not submit the form but still allow enter to be pressed?
    – Grady D
    Commented Sep 19, 2014 at 20:38
  • 1
    I changed the code a bit. Hope it works. Have a look at this, too: developer.force.com/cookbook/recipe/…
    – Uwe Heim
    Commented Sep 19, 2014 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .