Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get selected index of checked radio button

Status
Not open for further replies.

sourabhjha

Programmer
Jan 13, 2002
121
IN
i have a requiresment where i have to get the index of the checked radio button on form load...and capture that number to recognise that element on some button click function on the form..anyhelp
 
var el = document.forms[0].myRadioButtonName;

for (x = 0; x < el.length; x++)
if (el[x].checked)
return x;
=========================================================
if (!succeed) try();
-jeff
 
You can modify this to suit your needs. &quot;radioObject&quot; is the radio button input. This script gets the value that's selected; so instead of proceeding to get the value, when it gets to the checked option you'd want to return i (try, value = i and change the return value line to read &quot;return i&quot;.

function getRadioValue(radioObject){
var value = null;
for (var i=0; i<radioObject.length; i++){
if (radioObject.checked){
value = radioObject.value;
break}
}
return value;
}
 
oops - should wrap it in a function:
[tt]
function getIndex(o) {
for (x = 0; x < o.length; x++)
if (o[x].checked)
return x;
}

[/tt]
call it like so:[tt]
var n = getIndex(document.forms[0].myRadioName);[/tt] =========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top