• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Focus the first element

 
Ranch Hand
Posts: 86
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm creating a simple login screen and I need to focus on the first field "login" after I click a submit button, so when the page return. I tryed using JS :

but it not worked. I need a solution if possible without using JS. I'm using raw JSF implementation without PrimeFaces or anything else like this.
Thanks in advance!
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no other way to set focus on a web page than JavaScript. JSF is at the mercy of the limitations of HTML and HTML doesn't have such an option.

The reason that your JavaScript failed is because the IDs and structures of JSF's View Template Language (xhtml) are abstract and don't directly correspond to those in the rendered HTML and JavaScript must operate based on what's in the rendered HTML. Use your browser's "View Page Source" tool to see what actually got rendered and code your JavaScript accordingly. Note that if you do not explicitly code id's on your controls and their "naming containers" (forms, tables and so forth) that JSF with automatically synthesize IDs (j_123) but those IDs are neither constant nor predictable, unlike explicitly-coded IDs. Also that the ID of the rendered HTML is a concatenation of the control's ID plus the IDs of its parent naming containers.
 
Vinicius Souza
Ranch Hand
Posts: 86
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:There is no other way to set focus on a web page than JavaScript. JSF is at the mercy of the limitations of HTML and HTML doesn't have such an option.

The reason that your JavaScript failed is because the IDs and structures of JSF's View Template Language (xhtml) are abstract and don't directly correspond to those in the rendered HTML and JavaScript must operate based on what's in the rendered HTML. Use your browser's "View Page Source" tool to see what actually got rendered and code your JavaScript accordingly. Note that if you do not explicitly code id's on your controls and their "naming containers" (forms, tables and so forth) that JSF with automatically synthesize IDs (j_123) but those IDs are neither constant nor predictable, unlike explicitly-coded IDs. Also that the ID of the rendered HTML is a concatenation of the control's ID plus the IDs of its parent naming containers.



Ok, I already done that and I saw my JS untouched and it should work, see:

As you can see I overrided window.onload event and focused on the element ID "form:usuario", why it isn't working?
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing obviously wrong there to me.

There are some possibilities, however.

First, the ":" as an identifier name element can annoy some processors. I DON'T think JavaScript has a problem, but CSS sometimes does. In which case a solution such as using the form "form\:usario" might work. Although I've seen other syntax variations as well. I have no "master document" that tells me for certain what works where, however.

Secondly, the "onload" function sometimes has issues because it's not completely synchronous with page load and rendering. One thing you might try is to move the onload() call down to the bottom of the page to help ensure that the control you are focusing on has been rendered in time.
reply
    Bookmark Topic Watch Topic
  • New Topic