« XPages in Pictures - 08 - Return to Company | Main| Announcing Notes in Nine »

Getting the value of a Radio Button in Xpages.

Category Xpages
This is a long story for a relatively short answer.  Sorry about that...

Recently I was working on a test XPage application and wanted to have a page that would let the users fill out some fields and then it would create a document in the "backend" using server side Javascript and send the document to a team HelpDesk e-mail account. I would not be saving this document in the application.

On this page I wanted to have a simple Radio button with the choices Low, Medium, and High.  The user would choose the level of their problem and I'd include that in the e-mail.  However, I hadn't used Radio Buttons up to that point.  Especially in the case that I wasn't really saving the XPage.  I only wanted to get the value to the backend document that would be mailed.

I had some problems trying to figure out how to get the value.  You see, when you're dealing with individual radio buttons on an XPage, each option is it's own component and when you drop them on a page each has it's own unique name. This is not like the Notes Client where you have one Field of the type "Radio" with multiple options inside it.  Hmmm..  I know in traditional Javascript you need to iterate through a control looking for the checked property.  But what do you do here?

I first tried to get the value of the selected button by trying to do a getComponent on the group name.  

var choice = getComponent("groupName")

Well that didn't get me to far.  So I shouted out on twitter and got some good suggestions.  The best might have been to ask Matt "Mr. XPages" White himself, but really this has to be solvable by us mere mortals. Paul Hannan noticed that I didn't have "getValue" at the end of my statement.  Whoops.  I do keep forgetting that so I gave this a try:

var choice - getComponent("groupName").getValue()

Still no Joy.  Declan suggesting I look at the Domino Designer Wiki which I did go back to.  There's some articles on Radio Buttons but they seem to be talking about a Control in the Other Controls section called a Radio Group.  I still don't know what that is.  Something to do with not having hard coded values but looking them up from a view I think.  That didn't seem right so I moved on.

Back to playing with my XPage I noticed that even though I didn't really want this page bound to a Notes Form, it was telling me to.  These radio buttons wanted to be bound to something.  Ok who am I to argue with DDE? So I created a form and bound the 3 radio buttons in the group to the same field.

That's when something really interesting happened!  I tried this code:

var choice1 = getComponent("radio1").getValue();
var choice2 = getComponent("radio2").getValue();
var choice3 = getComponent("radio3").getValue();

And guess what?  Each variable contained the SAME value.  So to get the Value of a radio button in XPages, just get the value of ANY of the controls in a Radio Group!  

How cool is that?  No looping to find a value like in normal JavaScript.


XPages are like wives.  They can be frustrating at times, but at the end of the day, you're glad to be with them.

 

Comments

Gravatar Image1 - Do you have a more complete example? I am fighting this to migrate a notes form to XPages, but the radio buttons are holding me back. I can't seem to get the value of the radio button. I use js to update values on a notes form, but the same code does not seem to work on the xpages.

TIA

Gravatar Image2 - I tested your method for radio buttons and it worked great. I also tried the radio buttons without binding their values to a field on a form and it worked perfectly! I just put them in the same radio group. I must say that I'm using Lotus Domino Designer 8.5.1, but I'm not sure that's because of it that it works.

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Powered By:

Domino BlogSphere
Version 3.0.2