0
votes

I press commandbutton to need focus specify id not working in primefaces. I try this below code:

<h:form id="mainForm">
    <p:inputText id="userLoginNameTextId"/>
    <p:inputText id="passwordTextId"/>
     <p:commandButton id="userAddButton" value="#{platform.addButton}"
     onfocus="userLoginNameTextId" action="user.addAction"/>
</h:form>

Note: I tried the below attributes in onfocus

  1. onfocus="#mainForm:userLoginNameTextId"
  2. onfocus="#mainForm:userLoginNameTextId"
  3. onfocus="document.getElementId(mainForm:userLoginNameTextId)"
2

2 Answers

0
votes

The onfocus is an event that will fire if you focus the commandbutton (and e.g. execute some javascript). It is not something to be used to set focus to something else.

Do you realy need to set focus to an input clientside when you click a button?

0
votes

Try this with p:focus

<p:panel id="addPanel">
            <p:focus context="addPanel" />
            <h:panelGrid columns="3" cellpadding="9" cellspacing="9">
                <f:facet name="header">
                    <p:messages style="text-align:left;" styleClass="messages"
                        errorClass="invalid" infoClass="valid" warnClass="warning"
                        globalOnly="true" />
                </f:facet>

                <p:outputLabel for="typeName" value="Type Name:" />
                <p:inputText id="typeName" required="true" />
                <p:message for="typeName" errorClass="invalid" />

                <p:outputLabel for="typeValue" value="Type Name:" />
                <p:inputText id="typeValue" required="true" />
                <p:message for="typeValue" errorClass="invalid" />

                <p:spacer></p:spacer>
                <h:panelGrid columns="1" id="addTypeAction">
                    <p:commandButton id="addButton" value="Save" styleClass="register"
                        update="@form" />
                </h:panelGrid>
                <p:spacer></p:spacer>
            </h:panelGrid>
        </p:panel>