Jasmin Bhatti
Jasmin Bhatti

Reputation: 57

Submitting form by hitting ENTER event in primefaces p:autoComplete

Below is my code:

<h:form>   
  <p:autoComplete id="autoCompleteID" value="#{myBean.item}"  
                       completeMethod="#{myBean.completeMethod}"    
  </p:autoComplete>
  <h:commandButton action="#{myBean.searchRelatedItems}"/>
</h:form>

Here my scenario is like standard Google search, I can see related Items in suggestion and also redirect another page based on text typed in p:autoComplete text field. Its works fine by click on Button, but I also want to achieve by hitting ENTER in p:autoComplete text field.

Upvotes: 2

Views: 1948

Answers (1)

Theofanis
Theofanis

Reputation: 637

A late answer, but I will just leave it here...

  • Try using a Primefaces commandButton, it is p:commandButton, not h:commandButton.
  • Make sure the attribute type of the commandButton is "submit" and not "button", "submit" is the default in Primefaces.
  • Lastly a certain (but nasty I believe) solution is to put an id at the commandButton like "search-button" and an id at the h:form like "searchForm" and put the following at the p:autoComplete or h:form onkeyup="if (event.keyCode === 13) { document.getElementById('searchForm:search-button').click(); return false; }"

Similar topic: Submit form with primefaces widgets on enter

Upvotes: 1

Related Questions