/    Sign up×
Community /Pin to ProfileBookmark

Checking which radio button is checked

I have multiple radio buttons, all with the same inout tag name, so that they are mutually exclusive of each other. How can Javascript check to see WHICH button is actually checked? All <input> tags have the same name, but different values, so I thought that

myform.myradio.value

will give me the value of the radio button actually checked. Instead, it gives me undefined.

Any clues?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Jul 11.2004 — Use the HTML collection, by indexing it as an array. For example,alert(document.myForm.radios[0].checked);
alert(document.myForm.radios[1].checked);
alert(document.myForm.radios[2].checked);
alert(document.myForm.radios[3].checked);
will take the following form:&lt;form name="myForm"&gt;
&lt;input type="radio" name="radios" value="Red"&gt;Red&lt;BR&gt;
&lt;input type="radio" name="radios" value="Orange"&gt;Orange&lt;BR&gt;
&lt;input type="radio" name="radios" value="Blue"&gt;Blue&lt;BR&gt;
&lt;input type="radio" name="radios" value="Green"&gt;Green&lt;BR&gt;
&lt;/form&gt;
After looping thru the collection to determine the radio checked, you can alert its value:for(var i=0;i&lt;document.myForm.radios.length;i++) {
if(document.myForm.radios[i].checked) {
break;
}
}
alert("You selected: "+ document.myForm.radios[i].value;)
Dr. Script
×

Success!

Help @karayan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...