intelliWork
intelliWork

Reputation: 185

JavaScript - Checkbox like radiobutton functionality

How i can code the functionality of checkbox like radiobutton,below is mine quick code but it's seems not so perfect.

<asp:checkbox id="chkA" runat="server" onClick="SetCheck(this)"/>
<asp:checkbox id="chkB" runat="server" onClick="SetCheck(this)"/>

<Javascript>
function SetCheck(chk)
{
Switch(chk.id)
{
case "chkA" :
chkB.checked=false;
break;
Case "chkB" :
chkA.checked=false;
break;
}
}
</Javascript>

Upvotes: 0

Views: 471

Answers (1)

epascarello
epascarello

Reputation: 207511

HTML

<input type="checkbox" id="a" onclick="flip(this)" />
<input type="checkbox" id="b" onclick="flip(this)" />

JavaScript: Flips to other

function flip(cb){
    var other = cb.id==="a" ? "b" : "a";
    document.getElementById(other).checked = !cb.checked;
}

Example

JavaScript" Allows to be deselected

function flip(cb){
    if (cb.checked) {
        var other = cb.id==="a" ? "b" : "a";
        document.getElementById(other).checked = false;
    }
}

Example

Upvotes: 1

Related Questions