3 Replies Latest reply on Oct 11, 2007 5:46 AM by dmitry.demyankov

    Set focus on input field, what to do?

    mtaal

      Hi,
      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:
      $("formID:firstFieldId").focus();

      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.

      Thanks!

      gr. Martin

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

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

          
          <h:form>
          
          <!-- add controls here -->
          <a4j:outputPanel ajaxRendered="true">
           <h:panelGrid columns="2">
          
           <h:outputLabel for="ParameterName" value="Parameter Name:" styleClass="label-bold" />
           <h:panelGroup>
           <h:outputText id="ParameterName" value="#{operatorForm.paramName}" styleClass="label"/>
           </h:panelGroup>
          
           <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"/>
          <script>
          document.getElementById('ParameterValue').focus();
          </script>
          
          </h:panelGrid>
          </a4j:outputPanel>
          </h:form>
          
          


          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?
            mtaal

            Hi,
            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:
            j_id66:books:bookTitleDecoration:title
            (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

              Try setting

              prependId="false"
              if you're using h:form