Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:04:00+00:00 2026-05-15T15:04:00+00:00

I want to set focus on inputText field in JSF on page load. How

  • 0

I want to set focus on inputText field in JSF on page load. How can I implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:04:01+00:00Added an answer on May 15, 2026 at 3:04 pm

    If you’re already at HTML5 and JSF 2.2, use HTML5 autofocus attribute. In JSF 2.2 you can set it as a passthrough attribute.

    <html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
    ...
    <h:inputText ... a:autofocus="true" />
    

    You can even conditionally set it, you only need to make sure that the false condition evaluates to null, otherwise the attribute will still be rendered. It’s a HTML boolean attribute, so only its presence is already the trigger regardless of its value. When the value is explicitly set to null, then JSF won’t render the attribute in its entirety.

    <h:inputText ... a:autofocus="#{component.valid ? null : true}" />
    

    An alternative would be to throw in some JavaScript. Every input element has a focus() function. The below naive Vanilla JS approach focuses the first element of the first form.

    window.onload = function() {
        document.forms[0].elements[0].focus();
    }
    

    If you want to focus a specific input element during window load, then do:

    window.onload = function() {
        document.getElementById('formId:inputId').focus();
    }
    

    If you happen to have jQuery at hands, more fine grained checks could be done.

    $(document).ready(function() {
        $(":input:visible:enabled:first").focus()
    });
    

    In case you intend to execute it only after a postback, head to below related Q&A:

    • Set focus to inputText on validation error
    • Calling a JavaScript function from managed bean

    Utility/component libraries may have builtin autofocus facilities. OmniFaces has a <o:highlight> which highlights all invalid inputs on postback and autofocuses the first one. PrimeFaces by default autofocuses the “last active” element on complete of an ajax request and has a <p:focus> for more fine grained control. See also below related Q&A:

    • OmniFaces highlight does not set focus with <p:ajax>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I want to set focus on <h:inputText> element whenever rich:panelBarItem is opened. I
What I want to do is set the focus to a specific control (specifically
I want to set focus on the first form element in a div, either
I want to set focus to an MDI Parent Form when I click on
I'm using multiple sheets within my Excel application and want to activate (set focus
It's fairly straightforward. I want to set focus to the first enabled and not
I have a form and want to set the initial focus to a text
I have a textbox which I want to set the focus on, but it
I have a form and I want to set the focus to a text
I have an application developed in netbeans and I want to set the focus

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.