Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to uncheck a checkbox when another is checked

This is a MVC 3 application that uses the razor view engine. I have the following line of code in a view that displays 2 checkboxes. When one is checked the other checkbox should uncheck. I have searched around and between my lack of exp with javascript and the lack of threads found on google related to this i am at a lost for even a code snip of javascript to use for a worthwhile starting point. Any ideas?? This below is what I have coded up on my own but am more stuck than anything with this.

        $("#isStaffCheckBox").change(
              function (e) {
                  var checked = $(this).attr('checked');
                  if(checked)
                  {

    <li>Staff Member: @Html.CheckBoxFor(Function(f) f.isStaff, New With {.id = "isStaffCheckBox"}) Board Member: @Html.CheckBoxFor(Function(f) f.boardMember, New With {.id = "isBoardCheckBox"})</li>

Any help is greatly appreciated..

like image 203
Skindeep2366 Avatar asked Mar 24 '23 20:03

Skindeep2366


2 Answers

If there is some reason for this to be a checkbox instead of a radio button then the following code should do it.

    $("#isStaffCheckBox").change(
          function (e) {
              $("#isBoardCheckBox").prop('checked',!this.checked);
          }
    });
like image 155
HMR Avatar answered Mar 26 '23 10:03

HMR


Change the property "("#isBoardCheckBox").prop" to "("#isBoardCheckBox").attr".

Works fine!

  $("#isStaffCheckBox").change(
  function (e) {
      $("#isBoardCheckBox").attr("checked", !this.checked);
  })
like image 36
Fábio Avatar answered Mar 26 '23 11:03

Fábio