3 Replies Latest reply on Oct 11, 2007 5:46 AM by Dmitry Demyankov

    Set focus on input field, what to do?

    Martin Taal Newbie

      I want to set the focus to a specific input when a form opens. There are a number of posts on this forum about focus but it did not solve it for me (and I don't want to use myfaces).
      One post suggested to do this:

      but I don't understand how/why this would work and it did not work with me.

      As another solution is there for example an easy-to-use richfaces js function, like getElement(...) which returns the input dhtml object when passing the id? With this I can do focus explicitly as a jscript call.


      gr. Martin

        • 1. Re: Set focus on input field, what to do?
          Luke Bishop Newbie

          I was able to set focus on an input field within richfaces modalpanel (facelets):

          <!-- add controls here -->
          <a4j:outputPanel ajaxRendered="true">
           <h:panelGrid columns="2">
           <h:outputLabel for="ParameterName" value="Parameter Name:" styleClass="label-bold" />
           <h:outputText id="ParameterName" value="#{operatorForm.paramName}" styleClass="label"/>
           <h:outputLabel for="ParameterValue" value="Parameter Value:" styleClass="label-bold" />
           <t:inputText id="ParameterValue" value="#{operatorForm.paramValue}" styleClass="label" size="55" maxlength="64" forceId="true"/>

          The modal panel show logic required the focus attribute to get this to work with IE:

          <a4j:commandLink action="#{operatorForm.editParameterAction}" focus="ParameterValue" onclick="javascript:Richfaces.showModalPanel('paramDlg');" rendered="#{empty sequence.completionDate}">

          • 2. Re: Set focus on input field, what to do?
            Martin Taal Newbie

            Thanks for your reply. I am not sure it will work in my case though as the id of the input's get pre-pended with their container id's. Here is an example id:
            (I also use Seam's s:decorate)

            As I want to set focus when the form opens I can not use the focus attribute of the richfaces components (afaik).

            However as richfaces has this feature of focus in several components there is maybe an easy-to-use jscript function or something which I can use to get to an input without needing to know all the id encoding strategies.

            gr. Martin

            • 3. Re: Set focus on input field, what to do?
              Dmitry Demyankov Novice

              Try setting

              if you're using h:form