...

View Full Version : Select radio button with onclick event



Michiel
01-17-2005, 05:59 PM
Hi there,

I was wondering how one can select a radio button when a certain checkbox is clicked. I've got two radio buttons (one with value 1 and the other with value 2). Furthermore I've got three checkboxes that are positioned in a way that they appear as subitems for the second radio button. Now I would like to enhance that effect by selecting the second radio button whenever one of the checkboxes are clicked.

How does one do this? I already tried assigning a value to the radio button with an onclick event (onclick="document.form.B2.value = 2"), but this doesn't seem to work.

Anyone know how to do this?

Thanx, Michiel

Mr J
01-17-2005, 06:34 PM
Please try the following


<script>
<!--

function chkme(){
count=0
for(i=0;i<3;i++){
if(document.myform.elements["c"+i].checked==true){
document.myform.r1[1].checked=true
}
else{
count++
}
}
if(count==3){
document.myform.r1[0].checked=true
}
}

//-->
</script>

<form name="myform" >
<input type="radio" name="r1">
<input type="radio" name="r1">

<input type="checkbox" name="c0" onclick="chkme()">
<input type="checkbox" name="c1" onclick="chkme()">
<input type="checkbox" name="c2" onclick="chkme()">
</form>

Badman3k
01-17-2005, 07:00 PM
Alternatively you could do it this way:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
<p>
<label>
<input name="RadioGroup1" type="radio" value="1" checked>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="2">
Radio</label>
<br>
</p><label><input name="box1" type="checkbox" onclick="document.form1.box1.checked == true?document.form1.RadioGroup1[1].checked=true:document.form1.RadioGroup1[0].checked=true;" value="">
Testing...</label>
</form>
</body>
</html>

Mr J
01-17-2005, 08:30 PM
Badman3k

I think you forgot to take into account that there is going to be 3 checkboxes

Badman3k
01-17-2005, 11:08 PM
That's a good point, I did neglect to consider the possibility that although one may be being unselected this doesn't mean that others are not selected.

Thanks Mr J :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum