Hachim AlaOui Hachim AlaOui - 2 months ago 6
Javascript Question

Looking for a solution to link checkboxes

I need some help because I’m not that experienced with checkboxes.

So, I have a list and each row have a checkbox, by the way this list is auto generated but whatever, what I want is to be able to create some group or a link between some of the checkboxes, that if I check one of them, the others get checked too, of course only the ones linked to that one that I checked.

If it's not possible please let me know.

I hope that you understood what I wrote, and sorry for my bad English. Thank you in advance!

ps: i'm using it in a JSP

zpc zpc
Answer

Is it your expect? like follow code

 <script>
function f(vobj,vname){
    if(vobj.checked){
        checkoruncheck(vname,'checked');
    }else{
        checkoruncheck(vname,'unchecked');
    }

}
function checkoruncheck(vname,vf){
    var boxs=document.getElementsByName(vname);
    for(i=0;i<boxs.length;i++){
        if(vf==='checked'){  
            boxs[i].checked=true;
        }else{
            boxs[i].checked=false;
        }

    }   
}
</script>
<input type="checkbox" name="checkboxname" onchange="f(this,'checkboxname')"> checkbox A <BR>
<input type="checkbox" name="checkboxname" onchange="f(this,'checkboxname')"> checkbox B <BR>
<input type="checkbox" name="checkboxname" onchange="f(this,'checkboxname')"> checkbox C <BR>
<input type="checkbox" name="checkboxname" onchange="f(this,'checkboxname')"> checkbox D <BR>