/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] How do you set a radio button with JS?

Is it possible to set a radio button option using JS?

Either using a group ‘name’ or some unique ‘id’?

I can check the status of the radio button group with JS,
but I can’t figure out how to set a particular option without using HTML.

Purpose: While testing a form input, I would like to set some radio buttons
to a particular state without altering the <option> tags with a ‘checked’ parameter,
so that I would not have to go back and erase them after testing.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 21.2009 — document.getElementById('xxxx').checked=true;
Copy linkTweet thisAlerts:
@JMRKERauthorApr 21.2009 — document.getElementById('xxxx').checked=true;[/QUOTE]

Using that method, do I create a unique ID for each option of the radio buttons?

For example:
<i>
</i>&lt;input type="radio" name="q1' id='q1_5' value="5"&gt;Strongly Agree
&lt;input type="radio" name="q1' id='q1_4' value="4"&gt;Agree
&lt;input type="radio" name="q1' id='q1_3' value="3"&gt;Neutral
&lt;input type="radio" name="q1' id='q1_2' value="2"&gt;Disagree
&lt;input type="radio" name="q1' id='q1_1' value="1"&gt;Strongly Disagree
&lt;input type="radio" name="q1' id='q1_0' value="0"&gt;No Opinion


I had tried to do this, but could never get the right syntax:
<i>
</i>function SetRBtn(GrpName,GrpOption) {
document.form.GrpName[GrpOption].checked = true;
}

// ... tried calling with ...

&lt;button onClick="SetRBtn('q1',2)"&gt;Set choice(s)&lt;/button&gt; &lt;!-- initialize to neutral --&gt;

Any ideas about what I was doing wrong?

Thanks for the alternative 'Fang'.


I'll give it a try using the following after modifying my HTML to add unique ID values to options.
<i>
</i>function SetRBtn(GrpIdOption) {
document.getElementById(GrpIdOption).checked = true;
}
Copy linkTweet thisAlerts:
@FangApr 21.2009 — function SetRBtn(GrpName,GrpOption) {
document.form.elements[GrpName][GrpOption].checked = true;
// or
document.getElementsByName(GrpName)[GrpOption].checked = true;
}

Also match the quotes:&lt;input type="radio" name="q1[COLOR="Red"]'[/COLOR] id='q1_5' value="5"&gt;Strongly Agree

Button default is submit, add type:&lt;button type="button" onclick="SetRBtn('q1',2)"&gt;Set choice(s)&lt;/button&gt;
Copy linkTweet thisAlerts:
@ZeroKilledApr 21.2009 — edit:

never mind, too late for me! ?
Copy linkTweet thisAlerts:
@JMRKERauthorApr 21.2009 — Thank you 'Fang'. ?

And thanks for the attempt 'ZeroKilled'. ?
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...