1

I am using JSF 1.2 and RichFaces. I have a requirement where on click of command button (<h:commandButton>) RichFaces data table will be displayed with 3 set of rows, This is working fine but the problem is I need to set the focus in the text box of data table which is not working. Please find the sample code I am working with.

Command button :

<a4j:commandButton immediate="true"
                   action="#{bean.addTomethod}"
                   reRender="myform" 
                   oncomplete= "setFocusOnRichComponenet();" 
                   rendered="true">
</a4j:commandButton>

JavaScript code :

function setFocusOnRichComponenet(){
    document.getElementById("myform:richDataTableList:0:richTextBoxID").focus();
}

Here, myform:richDataTableList:0:richTextBoxID is the id of the component where I want to put focus.

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Are you getting any Javascript error ? In the oncomplete attribute you have specified the function name as `setFocusOnRichComponent` however, the javascript function name is `setFocusSPP`. Can you check by keeping the function name same ? – Dinesh Chitlangia Aug 13 '15 at 06:36
  • Sorry Dinesh that is typo mistake, function name is same . even though it is not working – user3718242 Aug 13 '15 at 08:51

2 Answers2

1

When the id is generated dynamically, it can be erroneous at times to try to refer using document.getElementById as you must know the full id.

Simpler way to do this is use the RichFaces method clientId('elementName') This will basically resolve the exact id so you don't have to refer by tableName[].Element[] format.

So for your scenario, you could replace the javascript function as below:

function setFocusOnRichComponenet(){
    document.getElementById("#{rich:clientId('myTextBox')}").focus();
}

where 'myTextBox' is the name of the textbox you are trying to refer

0

Add a class in input:

<h:inputText id="test" class="testClass"/>

In function set focus by class with jQuery:

function setFocusOnRichComponenet(){
    jQuery(".testClass").focus();
}