Focal Point
[SOLVED] Javascript get value from Radio Button

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/623106342

September 18, 2009, 04:44 PM
Dan Pinault
[SOLVED] Javascript get value from Radio Button
Hi,

I have a radio button group on my html page that has three values. I'm trying to retrieve the value of the selected option using the code below but it doesn't work.
//Begin function radio1_onclick
function radio1_onclick(ctrl) {
var radBut = document.getElementById('radio1');
for (var i=0; i < radBut.length; i++)
   {
   if (radBut[i].checked == true)
      {
      alert(radBut[i].value);
      }
   }
}
//End function radio1_onclick


I get nothing. Not even a script error. Am I missing something?

Thanks,

Dan

This message has been edited. Last edited by: Dan Pinault,


7.7.05M/7.7.03 HF6 on Windows Server 2003 SP2 output to whatever is required.
September 18, 2009, 09:22 PM
Dave Ayers
Dan,

Here is some old code of mine that I think will get you going. The thing is to treat a radio group as an array.

     for (var i = 0; i < 3; i++) {
       if (document.formname.radioname[i].checked == true ){
	SelectedValue += document.formname.radioname[i].value ; 
	}
	}
 

This message has been edited. Last edited by: Dave Ayers,


Regards,
Dave

http://www.daveayers.com

WebFocus/Maintain 7.6.4-8
on Win2000 and 2003 Server
September 19, 2009, 03:00 AM
Anmol
Dan,
I got this working....perfectly...
 

 var radBut=document.form_name.radio1.length;
 alert(radBut);
 for (var i=0; i < radBut; i++)
 {
 if (document.form_name.radio1[i].checked==true)
    alert(document.form_name.radio1[i].value);
 }
}


please make sure to give the unique id for all the radio buttons as "radio1".......this may be the problem in your case.......
let us know if you got it working........
Smiler
regards,
Anmol.


WebFocus7.6.2, WebFocus 7.1.1,Windows
HTML, PDF and Excel
September 21, 2009, 10:37 AM
Dan Pinault
Thanks for the responses Dave and Anmol.

I don't know if it makes a difference or not but I'm using the radio button control from the html layout composer. It looks like this is behaving differently than a 'normal' radio button control.

My three options look like this...
<TR>
   <TD>
   <LABEL id=radio1_LABEL_0 style="CURSOR: default" for=radio1_0>
   <INPUT id=radio1_0 tabIndex=22 type=radio CHECKED value=SUM name=radio1 displaytext="Summary">Summary</LABEL></TD>
   <TD>
   <LABEL id=radio1_LABEL_1 style="CURSOR: default" for=radio1_1>
   <INPUT id=radio1_1 tabIndex=22 type=radio value=PRINT name=radio1 displaytext="Detail">Detail</LABEL></TD>
   <TD>
   <LABEL id=radio1_LABEL_2 style="CURSOR: default" for=radio1_2>
<INPUT id=radio1_2 tabIndex=22 type=radio value=COUNT name=radio1 displaytext="Count">Count</LABEL></TD></TR>


This looks to me like they are behaving like checkboxes. So, the code I ended up with is...
//Begin function radio1_onclick
function radio1_onclick(ctrl) {
if (document.getElementById('radio1_2').checked==true)
    {
    //Do this if true
    }
else
    {
    //Do this if false
    }
}
//End function radio1_onclick


The reason I am looking to see if the particular radio button (radio1_2) is checked is so I can [enable] or [uncheck and disable] a checkbox (checkbox8). The strange thing is that I need to reference the actual checkbox of checkbox8 as 'item1'! Go figure!!
//Begin function radio1_onclick
function radio1_onclick(ctrl) {
var chkBox = document.getElementById('checkbox8');
if (document.getElementById('radio1_2').checked==true)
	{
    chkBox.disabled=false;
	}
else
	{
	document.getElementById('item1').checked=false; 
	chkBox.disabled=true; 
	}
}
//End function radio1_onclick



Thanks for the help!

Dan


7.7.05M/7.7.03 HF6 on Windows Server 2003 SP2 output to whatever is required.
September 21, 2009, 01:35 PM
Dave Ayers
Dan,

Notice that while the IBI generated code has different "id=" values, the "name=" value is the same, 'radio1', for all the radio button elements, so you can use that to refer to the collection properties by name, not Id.


Regards,
Dave

http://www.daveayers.com

WebFocus/Maintain 7.6.4-8
on Win2000 and 2003 Server