Click here to Skip to main content
14,299,114 members

Make Checkbox behave like Radio button

khanishfaq82 asked:

Open original thread
I want a functionality using JS where my checkboxes will act something like radio buttons..

here i am pasting an example of the code..
<script>
function get()
{
 for (var loop=1; loop<=5; loop++)

                    {

                        if(document.getElementById('f'+loop).checked==true)

                        {

                            for (var loop1=1; loop1<=5; loop1++)

                            {

                                if(document.getElementById('f'+loop).id==document.getElementById('f'+loop1).id)

                                {

                                        document.getElementById('f'+loop1).checked = true;

                                }

                                else

                                {

                                        document.getElementById('f'+loop1).checked = false;

                                }

                            }

                        }

                    }



}

</script>
<input name="f[]" type="checkbox" id="f1" value="1" onClick="get()"/>
<input name="f[]" type="checkbox" id="f2" value="2" onClick="get()"/>
<input name="f[]" type="checkbox" id="f3" value="3" onClick="get()"/>
<input name="f[]" type="checkbox" id="f4" value="4" onClick="get()"/>
<input name="f[]" type="checkbox" id="f5" value="5" onClick="get()"/>



Now the point is that i can traverse it backwards like the radiobutton but i fail to do the same from top to bottom.. if some one of you would like to take a look then kindly copy and paste the code and see the problem. Simply what i want is that just one check should be selected from the group at one time..
Thanks in Advance..
:)
Tags: Javascript

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100