Javascript checkboxs

Busy's picture

He has: 6,151 posts

Joined: May 2001

Is it possible to do a radio form option with checkboxs using javascript ?

0 yes 0 no <-- radio, with same name it's one or the other
_ _
|_| |_| <-- checkbox, any can be selected

What I have is two checkboxs but only want one to be checked, javascript isn't my strong point so am a bit lost.
I'm trying to do something like (php) if(checkbox1[123] == "selected"){ checkbox2[123] == "unselect"; }

I can't use radio form buttons unless there is a way to unselect one of them. a normal radio option once clicked is either/or, no unselect option.

Or even a conformation alert on selection of the second checkbox if first is selected warning it could short out the universe Wink and on ok or cancel the second is unselected.

If only one is selected its fine, but not if both are.

druagord's picture

He has: 335 posts

Joined: May 2003

&lt;script language="Javascript"&gt;
function check_checkbox(intBox)
{
     if(document.myform.chkoption1.checked && document.myform.chkoption2.checked)
     {
            if(intBox==1)
            {
                  document.myform.chkoption2.checked=false;
            }
            else
            {
                  document.myform.chkoption1.checked=false;
            }
       }
}
&lt;/script&gt;
'
and in the page use

<form name="myform">
<input type="checkbox" name="chkoption1" onchange="check_checkbox(1)">
<input type="checkbox" name="chkoption2" onchange="check_checkbox(2)">
</form>
'

IF , ELSE , WHILE isn't that what life is all about

Busy's picture

He has: 6,151 posts

Joined: May 2001

Saving my bacon again druagord Smiling

I can't change the 'name' in the input tag as it's a database generated array, can I use id or something instead?
Another problem is there can be from one to a thousand and one sets of checkbox sets

druagord's picture

He has: 335 posts

Joined: May 2003

if you change the name just change it too in the script to add more checkbox youre id idea is a good one depending how it is set up you could loop trought all the id's with getelementbyid i won't write the code right now i would need my book for
that if nobody answered tomorow i'll give you something

IF , ELSE , WHILE isn't that what life is all about

druagord's picture

He has: 335 posts

Joined: May 2003

by the way i just remembered i needed something like that once i used radio button and added a choice named none i my case it solved the problem

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.