views:

150

answers:

2

Hi and thanks for your help,

I am trying to select a radio button based on an ASP variable. This variable directly correlates to the value field of the radio button.

Any ideas guys/gals?

Thanks, Will

A: 
var myradiogroup = document.getElementsByName('groupname of radio buttons');
                   // or document.forms['formname'].elements['radiogroupname'];
for (i=0; i<myradiogroup.length; i++){
     if (myradiogroup[i].value == '<%= myASPValue %>') myradiogroup[i].checked = true;
     else myradiogroup[i].checked = false;
}

remember, radio buttons come in groups... your question does not specify if you want that in server side or client side, i assumed its client side

Ayyash
+1  A: 

You can do it in the ASP Server-Side code by checking if the value of the radio button is equal to your variable.

<input type="radio" ... value="<%=sRadioButtonValue%>" <% If sValue = sRadioButtonValue Then Response.Write "checked=""checked""" %> />
Shawn Steward
Right, though for radio you want `checked` not `selected`
Dan
@Dan thanks, updated my answer. /brainFart
Shawn Steward
It should be checked="checked", not checked="true"
mgroves
@mgroves Actually it can be checked="anything"... all you need in html is the keyword "checked" but to be xhtml compliant you need to have attributes equal something. I typically use checked="checked" as well but it really doesn't matter.
Shawn Steward
not according to: http://www.w3schools.com/Xhtml/xhtml_syntax.asp - I understand that (many? most? all?) browsers don't care. I would appreciate a citation if I'm wrong.
mgroves
While it may work in most/all browsers, it is not semantically correct. I stand corrected.
Shawn Steward