There is no quite "easy" way. There is no ID to use the getElementById
function. However, the NAME attribute is set to the JSF client ID. So
if you know the full client ID, then you can use getElementsByName. If
you only know the component ID, but not the client ID, you can
"simply" get the form, then iterate through all the form elements
until you find one with the NAME that ends with that ID (checking the
word boundry in front of the ID if desired to avoid mis-matches).

I have made a simple function that does this. Note that it isn't the
best performance, but it is good when you don't know the client ID
(there are a few functions here):

///////////////////////////////////////////////////////////////////////
function getSelectedRadio(jsfId)
{
 var radios = getRadioButtons(jsfId)
 for (var i = 0; i < radios.length; i++)
 {
   if (radios[i].checked)
     return radios[i]
 }

 return null
}
///////////////////////////////////////////////////////////////////////
function getRadioValue(jsfId)
{
 var radio = getSelectedRadio(jsfId)
 return (radio == null) ? null : radio.value
}
///////////////////////////////////////////////////////////////////////
function getRadioButtons(jsfId)
{
 var radios = new Array()
 var frm = document.getElementById('myForm')

 for (var i = 0; i < frm.elements.length; i++)
 {
   var elem = frm.elements[i]
   if (!exists(elem.tagName) || elem.tagName.toUpperCase() != 'INPUT' ||
     !exists(elem.type) || elem.type.toUpperCase() != 'RADIO')
     continue

   if (!exists(elem.name) ||
     elem.name.indexOf(jsfId) == -1)
     continue

   radios.push(elem)
 }

 return radios
}




On 8/23/06, Jaya Saluja <[EMAIL PROTECTED]> wrote:
In Javascript

-----Original Message-----
From: Andrew Robinson [mailto:[EMAIL PROTECTED]
Sent: Wednesday, August 23, 2006 4:14 PM
To: MyFaces Discussion
Subject: Re: What is the best Ajax JSf framework to use?

In JavaScript or on the server?


On 8/23/06, Jaya Saluja <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
>
> I have added radio buttons to my application.
>
> Here's the code
>
>
>
> <h:selectOneRadio layout="pageDirection" >
>
>                 <f:selectItem itemValue="customers"
itemLabel="Customers"/>
>
>                 <f:selectItem itemValue="enabled_customers"
> itemLabel="Enabled Customers" />
>
>                 <f:selectItem itemValue="disabled_customers"
> itemLabel="Disabled Customers"/>
>
>  </h:selectOneRadio>
>
>
>
> How do I know which radio button is selected?
>
>
>
> Thanks,
>
> Jaya

Reply via email to