• Post Reply Bookmark Topic Watch Topic
  • New Topic

Focus the first element  RSS feed

 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!
 
Tim Holloway
Bartender
Posts: 18549
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 18549
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!