Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Automatically selecting a checkbox based on the value of a radio button

Reply
Thread Tools

Automatically selecting a checkbox based on the value of a radio button

 
 
newjazzharmony@hotmail.com
Guest
Posts: n/a
 
      03-21-2006
Hello group,

I want to automatically select a specific checkbox when a user clicks
(selects) a specific item in a radiobutton group. Both controls are in
the same form.

Let's say for argument's sake that the form looks like this
(inessential items left out for the sake of clarity):

<form name=form1>
<input type=radio name=Radio1 value=Option1>
<input type=radio name=Radio1 value=Option2>
<input type=checkbox name=Checkbox1>
</form>

I want to write some Javascript to automatically select the "Checkbox1"
checkbox when a user selects "Option1" in the radio button group.

It seems like this should be pretty simple, but I'm scratching my head
over it.

Thanks in advance!
Jonathan

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      03-21-2006
said on 22/03/2006 8:35 AM AEST:
> Hello group,
>
> I want to automatically select a specific checkbox when a user clicks
> (selects) a specific item in a radiobutton group. Both controls are in
> the same form.
>
> Let's say for argument's sake that the form looks like this
> (inessential items left out for the sake of clarity):
>
> <form name=form1>
> <input type=radio name=Radio1 value=Option1>
> <input type=radio name=Radio1 value=Option2>
> <input type=checkbox name=Checkbox1>
> </form>
>
> I want to write some Javascript to automatically select the "Checkbox1"
> checkbox when a user selects "Option1" in the radio button group.
>
> It seems like this should be pretty simple, but I'm scratching my head
> over it.


The trivial answer is to put an onclick attribute on the radio button to
check the checkbox:

<input type="radio" name="Radio1" value="Option1"
onclick="this.form.Checkbox1.checked=this.checked; ">


But when some other radio is selected, checkbox1 is not deselected.
Also, the checkbox can be unchecked even though the radio is still checked.

That can be 'fixed' by instead putting an onclick on the form that keeps
the two in sync, e.g.:

<form name="form1" action=""
onclick="this.Checkbox1.checked=this.Radio1[0].checked;">
...
</form>

But that seems rather pointless. Using onclick on one element to cause a
change in another can often lead to a very confusing interface. It is
usually used for 'select all' or similar functionality. I suspect there
is more to this than you are telling us - for example, if the radio is
clicked should a (sub)set of checkboxes be checked? If any of those
checkboxes is unchecked, should the radio be unchecked too?

What are you really trying to do?


--
Rob
 
Reply With Quote
 
 
 
 
newjazzharmony@hotmail.com
Guest
Posts: n/a
 
      03-22-2006
Rob,

Thanks for the response.
The essential reason that I posted the question was because I couldn't
figure out how to reference the checkbox from inside the onClick of the
radio button.
Unfortunately, the sample you posted (this.form.Checkbox1) doesn't seem
to be working.
Are you sure that syntax is correct?

Thanks,

Jonathan

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-23-2006
said on 23/03/2006 1:00 AM AEST:
> Rob,


Please quote what you are replying to - I posted two code snippets to be
used in different contexts. You may have used one in the other's
context. As you haven't quoted which bit you are replying to, I can't
tell what you are referring to.


> Thanks for the response.
> The essential reason that I posted the question was because I couldn't
> figure out how to reference the checkbox from inside the onClick of the
> radio button.
> Unfortunately, the sample you posted (this.form.Checkbox1) doesn't seem
> to be working.
> Are you sure that syntax is correct?


Yes, but it is not enough that that bit is correct. You need to either
quote what you are replying too or post the code that doesn't work.

A couple of tips: always ensure that your HTML is valid and that all
attribute values are quoted, even when not strictly necessary.


Consider the following forms (both tested in Firefox and IE):

<form name="form1" action=""
onclick="this.Checkbox1.checked=this.Radio1[0].checked;">
<div>
<input type="radio" name="Radio1" value="Option1">
<input type="radio" name="Radio1" value="Option2">
<input type="checkbox" name="Checkbox1">
</div>
</form>

Note that when included in the onclick attribute of the form, 'this'
refers to the form so 'this.Checkbox1' refers to the checkbox. It is
equivalent to - document.forms['form1'].Checkbox1.

Note also that because there are two radio buttons named 'Radio1',
this.Radio1 returns a collection so to get the first one, I've used
this.Radio1[0].

Also note that I think it's pretty impractical, but serves for
demonstration purposes.


Now consider this version:

<form name="form1" action="">
<div>
<input type="radio" name="Radio1" value="Option1"
onclick="this.form.Checkbox1.checked=this.checked; ">
<input type="radio" name="Radio1" value="Option2">
<input type="checkbox" name="Checkbox1">
</div>
</form>


Now 'this' refers to the radio button, not the form. So this.form
refers to the form attribute of the radio button, which is form1 and
this.form.Checkbox1 refers to Checkbox1.

Similarly, this.checked is the checked property of the radio button.

Also pretty useless in real life.


--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set 'disabled' attribute of 'input' based on value of radio button? David Javascript 5 07-28-2011 10:05 AM
selecting radio button in datalist Vikas Kumar ASP .Net 15 05-03-2006 05:27 AM
selecting the value of the node based on the value of parameter sp XML 3 02-07-2006 02:50 PM
Radio button List problem: How to find value of Radio button list's Selected Item using javascript?? Hiten ASP .Net Web Controls 1 05-26-2004 10:32 AM
Programmatically Selecting Item in Radio Button List John Criswell ASP .Net 1 07-24-2003 07:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57