Search code examples
ajaxjsf-2richfaces

How to use ajax in JSF inputtextbox to update h:selectOneRadio?


I was usinig JSF 2.0, richfaces, and a4j in my development. I have a radio button group that consist of 3 radio button, and there is a inputtextbox next to each of the radio button, just like the layout shown below:

radio button A | input textbox A
radio button B | input textbox B
radio button C | input textbox C

Whenever there is a focus on textbox, can this be done using ajax to update on radio button selection? E.g. when focus on input textbox B, then radio button B will be selected. May I know how this can be done?


Solution

  • There are many ways to this, first using plain javascript and using ajax on jsf or richfaces components. You can try using a4j:ajax nested inside your h:inputText and fire it whenever there is focus on your textbox.

    <h:selectOneRadio value="#{bean.value}" id="radio" />
    <h:inputText id="input" value="#{bean.textValue}" >
        <a4j:ajax event="onfocus" render="radio" action="{bean.setRadioSelected}"/>
    </h:inputText>
    

    On the action method set the value of selected radio to true. Hope this helps.