/    Sign up×
Community /Pin to ProfileBookmark

Can I select a checkbox using a radio button?

I have a series of checkboxes to indicate what colors I like from a selection. I have corresponding radio buttons to indicate which, of the colors I like, is my favorite.

When I select the radio button to choose my favorite color, I want to force the corresponding checkbox to ‘on’ if it isn’t already. How can I do this?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 17.2006 — can we see the essential HTML code (your checkboxes and radios)?
Copy linkTweet thisAlerts:
@Jack333authorJan 17.2006 — <FORM ACTION="../cgi-bin/mycgi.pl">

Which colors do you like?

<INPUT TYPE=CHECKBOX NAME="red" >red<BR>

<INPUT TYPE=CHECKBOX NAME="green">green<BR>

<INPUT TYPE=CHECKBOX NAME="yellow">yellow<P>

Which is your favorite color?

<INPUT TYPE=RADIO NAME="color" VALUE="red">red<BR>

<INPUT TYPE=RADIO NAME="color" VALUE="green">green<BR>

<INPUT TYPE=RADIO NAME="color" VALUE="yellow">yellow<P>

<INPUT TYPE=SUBMIT VALUE="submit">

</FORM>
Copy linkTweet thisAlerts:
@ziffgoneJan 18.2006 — Here you go:

<i>
</i>[COLOR=Blue]&lt;FORM ACTION="../cgi-bin/mycgi.pl"&gt;

Which colors do you like?&lt;br&gt;

&lt;INPUT id="red" TYPE=CHECKBOX NAME="red"&gt;[COLOR=Red]red[/COLOR]&lt;BR&gt;
&lt;INPUT id="green" TYPE=CHECKBOX NAME="green"&gt;[COLOR=Green]green[/COLOR]&lt;BR&gt;
&lt;INPUT id="yellow" TYPE=CHECKBOX NAME="yellow"&gt;[COLOR=Yellow]yellow[/COLOR]&lt;P&gt;

Which is your favorite color?&lt;br&gt;

&lt;INPUT TYPE=RADIO NAME="color" VALUE="red" onclick="checkIt(this.value);"&gt;[COLOR=Red]red[/COLOR]&lt;BR&gt;
&lt;INPUT TYPE=RADIO NAME="color" VALUE="green" onclick="checkIt(this.value);"&gt;[COLOR=Green]green[/COLOR]&lt;BR&gt;
&lt;INPUT TYPE=RADIO NAME="color" VALUE="yellow" onclick="checkIt(this.value);"&gt;[COLOR=Yellow]yellow[/COLOR]&lt;P&gt;

&lt;INPUT TYPE=SUBMIT VALUE="submit"&gt;

&lt;/FORM&gt;
[B][COLOR=Navy]&lt;script type="text/javascript"&gt;
function checkIt(val){
var inputs=document.getElementsByTagName('input');
for(i=0;i&lt;inputs.length;i++){
if(inputs[i].type=="checkbox"){
if(inputs[i].id==val){
inputs[i].checked=true;
}
else{
inputs[i].checked=false;
}
}
}
return true;
}
&lt;/script&gt;[/COLOR][/B][/COLOR]


The Script portion could also go in your "<head>~~</head>" section.

Hope that helps.

Regards...
Copy linkTweet thisAlerts:
@Jack333authorJan 18.2006 — I appreciate your help. Selecting a radio button now turns off all checkboxes, but does not turn on the associated one. I must appologize, as I fear I over-simplified my example. In fact my javascript is parsed with php, and I think your solution is conflicting with some already present 'onclick' funtionality. I confess, altough it must be obvious, I'm not a programmer-I know only enough to be dangerous.

$value=$data["Pref"];

while ($row = db_fetch_array($rstemp))

{

echo "rn<tr><td>";


if($row["ParticipantID"]==$value)
echo '<input type="Radio" name="radio'.$ifield.'" onclick="javascript: value'.$ifield.".value='".db_addslashes($row["ParticipantID"]).'';checkIt(this.value);return true;" checked>';
else
echo '<input type="Radio" name="radio'.$ifield.'" onclick="javascript: value'.$ifield.".value='".db_addslashes($row["ParticipantID"]).'';checkIt(this.value);return true;">';


echo "</td></tr>";

}
×

Success!

Help @Jack333 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.5,
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,
)...