Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have 5 jquery buttonset checkbox in my view
XML
<div id="format">
                <input type="checkbox" id="check2" onClick="disableButtons(id);"/><label for="check2">Check2</label>
                <input type="checkbox" id="check3" onClick="disableButtons(id);" /><label for="check3">Check3</label>
                <input type="checkbox" id="check4" onClick="disableButtons(id);"/><label for="check4">Check4</label>
                <input type="checkbox" id="check5" onClick="disableButtons(id);"/><label for="check5">Check5</label>
                <input type="checkbox" id="check6" onClick="disableButtons(id);"/><label for="check6">Check6</label>
</div>

and my Javascript code is
<script type="text/javascript">
    $(function () {
        $("#dynamicColumns,#format").buttonset();
    });

    function diableButtons(id) {
        //        alert(id.toString() + " Welcome");
        if (id == 'check2') {
            $('#check3').attr("disabled", true).button('refresh');
            $('#check4').attr("disabled", true).button('refresh');
            $('#check5').attr("disabled", true).button('refresh');
            $('#check6').attr("disabled", true).button('refresh');
        }
        if (id == 'check3') {
            $('#check2').attr("disabled", true).button('refresh');
            $('#check4').attr("disabled", true).button('refresh');
            $('#check5').attr("disabled", true).button('refresh');
            $('#check6').attr("disabled", true).button('refresh');
        }
       }
</script>

The above code works fine, but it wont look good if i check for every id.
Is there any optimize option to validate the condition?
When i uncheck the buttonset checkbox, other buttonset should reset to its default state.

For example:
If i select Check2 buttonset, other check buttonset(check3,check4,check5,check6) should be disabled.
And if i deselect check2 buttonset, other check buttonset(check3,check4,check5,check6) should be enabled.
Similarly for other check buttonsets too.
Please help me ASAP.
Thanks in advance.
Posted

1 solution

Sure, you can attach a handler to do all your enable\disable without checking each one. Have a look at this jsFiddle

http://jsfiddle.net/n4FxU/[^]

e.g. something like...

JavaScript
$('input:checkbox').click(function() {
    if ($(this).is(':checked')) {
        $('input:checkbox').attr("disabled", true);
        $(this).attr("disabled", false);
    } else {
        $('input:checkbox').attr("disabled", false);
    }
});
 
Share this answer
 
Comments
♥…ЯҠ…♥ 19-Oct-12 0:52am    
I've been searching for this man...... thanks a lot. Its working in jsfiddle but when i copied in my MVC project its is not working. Even i copied the html code too, but its not working. I hope function is not calling.... Any advice?
Dylan Morley 19-Oct-12 4:06am    
Open up your debugger (Firebug \ Chrome?), look for any script errors. There's nothing I can tell you that your debugger can't!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900