calvinfoo
asked on
Radio Button Selected Value
AFAIK, to retrieve the selected value for <SELECT> in a single line javascript, I have the following code:
this.form.areaID.options[t his.form.a reaID.sele ctedIndex] .value
If I were wanted to do the same, retrive the selected value for <RADIO> in a single line javascript, how?
this.form.areaID.options[t
If I were wanted to do the same, retrive the selected value for <RADIO> in a single line javascript, how?
ASKER
<SCRIPT LANGUAGE="JavaScript">
function openWindow(url, w, h, rs, sb) {
var windowprops = "width=" + w + ",height=" + h + ",resizable=" + rs + ",scrollbars=" + sb;
//windowprops += "center=yes;border=thin;he lp=no;stat us=no;loca tionbar=no ;statusbar =no;locati onbar=no;m enubar=no; toolbar=no ";
popup = window.open(url,'remote',w indowprops );
}
</SCRIPT>
<FORM ACTION="newsletter.asp" METHOD="post" NAME="formemail">
<INPUT TYPE="radio" NAME="format" VALUE="2">HTML
<INPUT TYPE="radio" NAME="format" VALUE="1" CHECKED>Plain Text
<TEXTAREA COLS="64" ROWS="12" NAME="bodytext"><%=strBody Text %></TEXTAREA>
<INPUT TYPE="Button" VALUE="Preview" ONCLICK="openWindow('newsl etter_prev iew.asp?bo dytext='+t his.form.b odytext.va lue+'&form at='+this. form.forma t.checked, 630, 400, 'yes', 'yes');">
</FORM>
note:
1. this sample form is simplified, may contains minor error
2. openWindow is a simplied javascript function of a pupup window
I tried your suggestion on "this.form.format.checked" , but doesn't seems to be working... :(
function openWindow(url, w, h, rs, sb) {
var windowprops = "width=" + w + ",height=" + h + ",resizable=" + rs + ",scrollbars=" + sb;
//windowprops += "center=yes;border=thin;he
popup = window.open(url,'remote',w
}
</SCRIPT>
<FORM ACTION="newsletter.asp" METHOD="post" NAME="formemail">
<INPUT TYPE="radio" NAME="format" VALUE="2">HTML
<INPUT TYPE="radio" NAME="format" VALUE="1" CHECKED>Plain Text
<TEXTAREA COLS="64" ROWS="12" NAME="bodytext"><%=strBody
<INPUT TYPE="Button" VALUE="Preview" ONCLICK="openWindow('newsl
</FORM>
note:
1. this sample form is simplified, may contains minor error
2. openWindow is a simplied javascript function of a pupup window
I tried your suggestion on "this.form.format.checked"
This probly isn't the best way to do what you want, but it gets it done.
<html>
<head>
<title>Selected Radio Buttons</title>
<script language="JavaScript">
<!--
function subForm()
{
var j = 0;
var radio_buttons = new Array();
var the_form = window.document.forms[0];
for(var i=0; i<the_form.length; i++)
{
var temp = the_form.elements[i].type;
if((temp == "radio") && (the_form.elements[i].chec ked)) { radio_buttons[j] = the_form.elements[i].value ; j++; }
}
for(var k=0; k<radio_buttons.length; k++)
{
document.write(radio_butto ns[k] + "<br>");
}
}
//-->
</script>
</head>
<body>
<form action="javascript: subForm();">
<input type="radio" name="r_1" value="A">A<br>
<input type="radio" name="r_1" value="B">B<br>
<input type="radio" name="r_1" value="C">C<br><br>
<input type="radio" name="r_2" value="D">D<br>
<input type="radio" name="r_2" value="E">E<br>
<input type="radio" name="r_2" value="F">F<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<head>
<title>Selected Radio Buttons</title>
<script language="JavaScript">
<!--
function subForm()
{
var j = 0;
var radio_buttons = new Array();
var the_form = window.document.forms[0];
for(var i=0; i<the_form.length; i++)
{
var temp = the_form.elements[i].type;
if((temp == "radio") && (the_form.elements[i].chec
}
for(var k=0; k<radio_buttons.length; k++)
{
document.write(radio_butto
}
}
//-->
</script>
</head>
<body>
<form action="javascript: subForm();">
<input type="radio" name="r_1" value="A">A<br>
<input type="radio" name="r_1" value="B">B<br>
<input type="radio" name="r_1" value="C">C<br><br>
<input type="radio" name="r_2" value="D">D<br>
<input type="radio" name="r_2" value="E">E<br>
<input type="radio" name="r_2" value="F">F<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
how about this.....
<SCRIPT LANGUAGE="JavaScript">
function openWindow(url, w, h, rs, sb) {
var windowprops = "width=" + w + ",height=" + h + ",resizable=" + rs + ",scrollbars=" + sb;
//windowprops += "center=yes;border=thin;he lp=no;stat us=no;loca tionbar=no ;statusbar =no;locati onbar=no;m enubar=no; toolbar=no ";
popup = window.open(url,'remote',w indowprops );
}
</SCRIPT>
<FORM ACTION="newsletter.asp" METHOD="post" NAME="formemail">
<INPUT TYPE="radio" NAME="format" VALUE="2" onclick="javascript:if(thi s.checked) {document. formemail. radioval.v alue='2';} ">HTML
<INPUT TYPE="radio" NAME="format" VALUE="1" CHECKED onclick="javascript:if(thi s.checked) {document. formemail. radioval.v alue='1';} ">Plain Text
<TEXTAREA COLS="64" ROWS="12" NAME="bodytext"><%=strBody Text %></TEXTAREA>
<INPUT TYPE="Button" VALUE="Preview" ONCLICK="openWindow('newsl etter_prev iew.asp?bo dytext='+t his.form.b odytext.va lue+'&form at='+this. form.radio val.value, 630, 400, 'yes', 'yes');">
<INPUT TYPE="hidden" name="radioval" value='1'>
</FORM>
With my solution you won't have to loop through the radio buttons.
just the hidden filed will do the trick..
Regards
Hart
<SCRIPT LANGUAGE="JavaScript">
function openWindow(url, w, h, rs, sb) {
var windowprops = "width=" + w + ",height=" + h + ",resizable=" + rs + ",scrollbars=" + sb;
//windowprops += "center=yes;border=thin;he
popup = window.open(url,'remote',w
}
</SCRIPT>
<FORM ACTION="newsletter.asp" METHOD="post" NAME="formemail">
<INPUT TYPE="radio" NAME="format" VALUE="2" onclick="javascript:if(thi
<INPUT TYPE="radio" NAME="format" VALUE="1" CHECKED onclick="javascript:if(thi
<TEXTAREA COLS="64" ROWS="12" NAME="bodytext"><%=strBody
<INPUT TYPE="Button" VALUE="Preview" ONCLICK="openWindow('newsl
<INPUT TYPE="hidden" name="radioval" value='1'>
</FORM>
With my solution you won't have to loop through the radio buttons.
just the hidden filed will do the trick..
Regards
Hart
ASKER
isn't there anyway direct get the value of the selected radio value like the example I have?
this.form.areaID.options[t his.form.a reaID.sele ctedIndex] .value
this.form.areaID.options[t
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great, now I understand the problem... :)
boy.. i hate javascript...
boy.. i hate javascript...
:-)
Regards
Hart
Regards
Hart
I realize that it is kind of late, but I just ran across this while looking for something else.
Anyway, how about this code (go to AllWordSearch.com to see the whole thing in operation):
<input type=radio name="search" value="words" checked>
<input type=radio name="search" value="phrase">
<input type=radio name="where" value="page" checked>
<input type=radio name="where" value="title">
search.cgi?search='+(searc h[0].check ed?search[ 0].value:' ')+(search [1].checke d?search[1 ].value:'' )
+'&where='+(where[0].check ed?where[0 ].value:'' )+(where[1 ].checked? where[1].v alue:'')
There's two sets of two radio buttons there, and it's all in a single line, just like you asked.
Anyway, how about this code (go to AllWordSearch.com to see the whole thing in operation):
<input type=radio name="search" value="words" checked>
<input type=radio name="search" value="phrase">
<input type=radio name="where" value="page" checked>
<input type=radio name="where" value="title">
search.cgi?search='+(searc
+'&where='+(where[0].check
There's two sets of two radio buttons there, and it's all in a single line, just like you asked.
I use an entirely different strategy:
I set up a global (and persistent) variable, say rdat, in a <head> javascript block
<script type='text/javascript' language='javascript1.2'>
rdat='';
...
</script>
then for each radio button I add a short onclick statement:
<input type=radio name='radbut' value='X' onclick='rdat=value'>
<input type=radio name='radbut' value='Y' onclick='rdat=value'>
...
This way the selected radio button is always available in the variable rdat. (Or you could use a hidden input value)
e.g. alert('radio button value='+rdat);
DLM
I set up a global (and persistent) variable, say rdat, in a <head> javascript block
<script type='text/javascript' language='javascript1.2'>
rdat='';
...
</script>
then for each radio button I add a short onclick statement:
<input type=radio name='radbut' value='X' onclick='rdat=value'>
<input type=radio name='radbut' value='Y' onclick='rdat=value'>
...
This way the selected radio button is always available in the variable rdat. (Or you could use a hidden input value)
e.g. alert('radio button value='+rdat);
DLM
@dennis_maeder: Why the JS 1.2 your code would work in any JS implementation
is true if the button is checked, false if not.
eg:
if (document.form.radioButton
// checked. Do this.
} else {
// not checked. Do that.
}
C:-)