gaffcz
gaffcz

Reputation: 3479

JSF form - submit > focus on selected input

I have this theoretical situation:

When I submit the form using btn1, I need to focus to inp1 (analogical btn2 to inp2, btn3 to inp3)

I'm able to use javaScript function like onclick="document.getElementById('frm:inp1').focus();" (it works)

Problem is, that submit button causes recreating of view and after that, javaScript setting is forgotten and component is not selected..

So how do I select a component which remains selected even after the view redraws?

Upvotes: 2

Views: 2443

Answers (2)

Dimitri Dewaele
Dimitri Dewaele

Reputation: 10689

Use the PrimeFaces component:

<!-- Focus on first field, or first field with error -->
<p:focus context="feesboek"/>

Use this in combination with a focus component and you will rock!

<!-- Default button when pressing enter -->
<p:defaultCommand target="submit"/>

Upvotes: -1

mrembisz
mrembisz

Reputation: 12880

You can include a script in your page which will update the focus after page is rendered. Update information about focused input on the server in action listeners. The script can look look like this with jquery:

<script>
  $(document).ready(function() { document.getElementById('frm:#{focusInfo.componentId}').focus(); })
</script>

It is assuming you have set componentId property in focusInfo bean in action/actionListener executed on server upon button click. FocusInfo contains componentId with getter and setter and possibly methods switching componentId and registered as actionListeners with your buttons.

Upvotes: 3

Related Questions